【问题标题】:Binding object to window textboxes in WPF将对象绑定到 WPF 中的窗口文本框
【发布时间】:2011-09-20 09:00:03
【问题描述】:

我有一个名为 Data 的类,其中有一些公共成员:NameAgeAddress

我还有带有文本框 NameAgeAddress 的窗口。

Data 对象可以随时更改。

如何将Data 对象绑定到文本框并在对象更改后跟随?

我知道有INotifyPropertyChanged 和“依赖属性”,但我不知道如何使用它们。

编辑

public class MyData : INotifyPropertyChanged
{
  private string _name;

  public string Name
  {
    get
    {
      return _name;
    }
    set
    {
      if (_name != value)
      {
        _name = value;
        OnPropertyChnged("Name");
      }
    }
   }
   public event PropertyChangedEventHandler PropertyChanged;

   protected void OnPropertyChanged(string name)
   {
     ProppertyChangedEventHandler handler = PropertyChanged;
     if (handler != null) handler(this, new PropertyChangedEventArgs(name));
   }
}

XAML 代码:

xmlns:myApp="clr-namespace:MyApp"
<Window.Resources><myApp:MyData x:key = data/></WindowResources>
<TextBox><TextBox.Text><Binding Source="{StaticResource data}" Path="Name" UpdateSourceTrigger="PropertyChanged"/></TextBox.Text></TextBox>

class OtherClass
{
  private MyData data;
  //the window that have the binding textbox
  private MyWindow window;
  public OtherClass()
  {
    data = new MyData();
    data.Name = "new name"
    window = new MyWindow();
    window.show();
  }
}

【问题讨论】:

  • 实现INotifyPropertyChanged 应该花费您 5 分钟:2 分钟 google-ing 和 3 分钟复制/粘贴到您的代码中...学习将您的 TextBox 绑定到对象的语法应该花费更短的时间:)
  • 其实我有 wpf 书,我在 google 上花了超过 5 分钟,我仍然不明白...这是我第一次使用 wpf,对我来说是全新的
  • 你能添加你的代码来定义静态资源数据吗?
  • 这个是否构建,因为您似乎有几个错字,在 OnPropertyChanged("Name") 中缺少 a 并且在 PropertyChangedEventHandler 中有一个额外的 p?
  • 这是一个不同的类实例,视图的静态资源正在构建它自己的类实例,因此您在 OtherClass 中创建的实例不会被使用。您可以摆脱 StaticResource,然后将窗口的使用 DataContext 设置为该类的实例,然后将绑定更改为

标签: wpf binding dependency-properties inotifypropertychanged


【解决方案1】:

这个来自 MSDN 的 link 解释得很好。

MSDN 链接已失效,正在添加类似article 的链接。

当你的类属性改变时,你的属性应该引发一个带有属性名称的 OnPropertyChanged 事件,以便 View 知道刷新它的绑定。

    public String Name
    {
        get { return _name; }
        set 
        {
            if (_name != value)
            {
                _name = value;
                this.OnPropertyChanged("Name");
            }
        }
    }

你的文本框应该有一个绑定,例如:

<TextBox Text="{Binding Name}"/>

我有一个 ViewModelBase 类,我在其中实现了我的 OnPropertyChandedEvent 以供所有派生模型调用:

    /// <summary>
    /// An event for when a property has changed.
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Virtual method to call the Property Changed method
    /// </summary>
    /// <param name="propertyName">The name of the property which has changed.</param>
    protected virtual void OnPropertyChanged(String propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

【讨论】:

  • 别忘了保存数据的对象(ViewModel)必须设置为XAML(View)的DataContext。
  • 没错,我以为发帖人已经知道了,但我应该明确说明。
  • @Paulie Waulie 我可能错过了一些东西。我做了你写的,在我创建和打开窗口之前,我在 Data.Name 属性中输入了一些值,但相关文本框中没有显示任何内容......有什么想法吗?
  • TextBox 上的绑定似乎不正确,您可以尝试下载此工具:wpftutorial.net/Inspector.html,附加到您的应用,然后按控制并将鼠标悬停在 TextBox 上,这将为您提供关于控制的大量细节。一个选项卡将是 DataContext,如果它为 null,则您尚未为视图设置数据上下文,否则请检查以查看该选项卡中属性的值。您如何为视图设置 DataContext?
  • @Maya 记得监视 Visual Studios 的输出框是否有所有 DataBinding 错误,它们会出现在那里。
【解决方案2】:

让我们 Data 类实现 INotifyPropertyChanged 。当有人更改 Data 实例的属性值时引发事件。然后为您的 UI 设置正确的DataContext,并绑定单个 ui 元素,例如:

<TextBox Text="{Binding Age}"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多