在 WPF 中将对象绑定到窗口文本框

发布于 2024-11-17 00:48:32 字数 1520 浏览 2 评论 0原文

我有一个名为 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();
  }
}

I have a class named Data with some public members: Name, Age, Address.

I have also window with text boxes Name, Age, Address.

The Data object can change any time.

How can I bind the Data object to the text boxes and follow after object changes?

I know there is INotifyPropertyChanged and "dependency-properties" but I do not know how to use them.

Edit

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 code:

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();
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰雪之触 2024-11-24 00:48:32

来自 MSDN 的链接很好地解释了这一点。

MSDN 链接已失效,添加链接到类似的 文章

当您的类属性更改时,您的属性应引发带有属性名称的 OnPropertyChanged 事件,以便视图知道刷新它的绑定。

    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));
    }

This link from MSDN explains it well.

MSDN link is dead, adding link to a similar article.

When your class property is changed, your property should raise a OnPropertyChanged event with the name of the property so that the View knows to refresh it's binding.

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

And your textbox should have a binding such as:

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

I have a ViewModelBase class which is where I have implemented my OnPropertyChandedEvent for all derived models to call:

    /// <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));
    }
皇甫轩 2024-11-24 00:48:32

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

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

Let's the Data class implements INotifyPropertyChanged . Raise the event when someone change the property value on the instances of Data. Then set the proper DataContext to your UI, and bind the single ui element as for example:

<TextBox Text="{Binding Age}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文