双向WPF中绑定 [英] Two-way binding in WPF

查看:185
本文介绍了双向WPF中绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能得到一个双向绑定在WPF工作。我有一个绑定到一个文本框在我的应用程序的主窗口中的字符串属性(我将模式设置为双向)。该文本框的值将更新的唯一时间是当窗口初始化。当我输入到文本框中,底层字符串属性值不会改变。当字符串属性的值是由外部源(在点击的事件,例如,刚刚重置文本框的值)更改,更改不会传播到文本框。

I cannot get a two-way bind in WPF to work. I have a string property in my app's main window that is bound to a textbox (I set the mode to "TwoWay"). The only time that the value of the textbox will update is when the window initializes. When I type into the textbox, the underlying string properties value does not change. When the string property's value is changed by an external source (an event on Click, for example, that just resets the textbox's value), the change doesn't propagate up to the textbox.

什么是我必须实现获得双向绑定正常工作的步骤,连这几乎是微不足道的例子吗?

What are the steps that I must implement to get two-way binding to work properly in even this almost trivial example?

(我可以提供code。如果真的有必要。我更期待的过程。我理解数据绑定的基础知识,但我很新的WPF。)

(I can provide code if it's really necessary. I'm more looking for the process. I understand the basics of data binding, though I'm very new to WPF.)

推荐答案

最可能的是你要绑定到.NET CLR属性,而不是一个WPF的DependencyProperty(提供更改通知此外还有一些其他的东西)。

对于普通的CLR属性,你需要实现INotifyPropertyChanged,并迫使在PropertyChanged的事件处理程序的文本框更新。

Most probably you're trying to bind to a .net CLR property instead of a WPF dependencyProperty (which provides Change Notification in addition to some other things).
For normal CLR property, you'd need to implement INotifyPropertyChanged and force update on the textbox in the event handler for PropertyChanged.


  • 因此,请与物业你的对象实现了这个接口,提高属性setter事件。 (所以现在我们有属性更改通知)

  • 确保该对象被设置为UI元素/控制
  • 的DataContext属性
  • So make your object with the property implement this interface, raise the event in the property setter. (So now we have property change notification)
  • Make sure the object is set as the DataContext property of the UI element/control

这把我摔下过,当我开始学习WPF数据绑定。

This threw me off too when I started learning about WPF data binding.

更新: 好了OP,它本来的时间,如果我是找错了树的浪费..反正现在你既然已经挖了一下..你会记得很长一段时间。的下面是code段四舍五入这个答案。同时发现,更新文本尽快自动发生,因为我制表了..你只需要手动订阅事件和更新用户界面,如果你的DataContext对象是不是一个执行INotifyPropertyChanged的。

Update: Well OP, it would have been a waste of time if i was barking up the wrong tree.. anyways now since you had to dig a bit.. you'll remember it for a long time. Here's the code snippet to round off this answer. Also found that updating the textbox happens automatically as soon as I tab-out.. You only need to manually subscribe to the event and update the UI if your datacontext object is not the one implementing INotifyPropertyChanged.

MyWindow.xaml

MyWindow.xaml

<Window x:Class="DataBinding.MyWindow" ...
    Title="MyWindow" Height="300" Width="300">
    <StackPanel x:Name="TopLevelContainer">
        <TextBox x:Name="txtValue"  Background="AliceBlue" Text="{Binding Path=MyDotNetProperty}" />
        <TextBlock TextWrapping="Wrap">We're twin blue boxes bound to the same property.</TextBlock>
        <TextBox x:Name="txtValue2"  Background="AliceBlue" Text="{Binding Path=MyDotNetProperty}" />
    </StackPanel>
</Window>

MyWindow.xaml.cs

MyWindow.xaml.cs

public partial class MyWindow : Window, INotifyPropertyChanged
{
    public MyWindow()
    {
        InitializeComponent();
        this.MyDotNetProperty = "Go ahead. Change my value.";
        TopLevelContainer.DataContext = this;
    }

    private string m_sValue;
    public string MyDotNetProperty
    {
        get { return m_sValue; }
        set
        {
            m_sValue = value;
            if (null != this.PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("MyDotNetProperty"));
            }
        }
    }

    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion
}

这篇关于双向WPF中绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆