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

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

问题描述

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

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?

(我可以提供代码,如果它是真的有必要,我更加期待这个过程,我了解数据绑定的基础知识,尽管我对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依赖属性(除了其他一些事情之外还提供了Change Notification)。

对于正常的CLR属性, '需要在PropertyChanged的事件处理程序中的文本框上实现INotifyPropertyChanged并强制更新。

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.


  • 所以使用属性实现这个接口的对象,在属性设置器中引发事件。 (所以现在我们有属性更改通知)

  • 确保将对象设置为UI元素/控件的DataContext属性

当我开始了解WPF数据绑定时,这也让我失望。

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

更新:那么OP,如果我正在咆哮错误的树,这将是浪费时间..无论如何,现在,因为你必须挖一点..你会记得很长一段时间。这是代码片段舍弃这个答案。还发现更新文本框一旦出现,就会自动发生..只需要手动订阅事件并更新UI,如果您的数据文本对象不是实现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天全站免登陆