为什么绑定更新不实现INotifyPropertyChanged? [英] Why does the binding update without implementing INotifyPropertyChanged?

查看:137
本文介绍了为什么绑定更新不实现INotifyPropertyChanged?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个ViewModel并将其属性绑定到UI上的两个文本框。当我将第一个和焦点的值更改为文本框之后,其他文本框的值会发生变化,但是我没有实现INotifyPropertyChanged。如何工作?

I created a ViewModel and bound its property to two textboxes on UI. The value of the other textbox changes when I change the value of first and focus out of the textbox but I'm not implementing INotifyPropertyChanged. How is this working?

以下是XAML

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:ViewModel />
    </Window.DataContext>
    <StackPanel>
        <TextBox Text="{Binding Name}" />
        <TextBox Text="{Binding Name}" />
    </StackPanel>
</Window>

以下是我的ViewModel

And following is my ViewModel

class ViewModel
{
    public string Name { get; set; }
}


推荐答案

是对的。现在我在网上搜索它,并发现这个

I tested it, you are right. Now i searched for it on the web, and found this.


对不起,要花很长时间回复,其实你正在遇到另一个 WPF的隐藏方面,这就是WPF的数据绑定引擎将数据绑定到PropertyDescriptor实例,如果源对象是纯CLR对象并且不实现INotifyPropertyChanged 界面。数据绑定引擎将尝试通过PropertyDescriptor.AddValueChanged()方法订阅属性已更改的事件。当目标数据绑定元素更改属性值时,数据绑定引擎将调用PropertyDescriptor.SetValue()方法将更改后的值传回源属性,同时引发ValueChanged事件通知其他用户(在这种情况下,其他订阅者将是ListBox中的TextBlock。

Sorry to take so long to reply, actually you are encountering a another hidden aspect of WPF, that's it WPF's data binding engine will data bind to PropertyDescriptor instance which wraps the source property if the source object is a plain CLR object and doesn't implement INotifyPropertyChanged interface. And the data binding engine will try to subscribe to the property changed event through PropertyDescriptor.AddValueChanged() method. And when the target data bound element change the property values, data binding engine will call PropertyDescriptor.SetValue() method to transfer the changed value back to the source property, and it will simultaneously raise ValueChanged event to notify other subscribers (in this instance, the other subscribers will be the TextBlocks within the ListBox.

如果您正在实施INotifyPropertyChanged,则您需要全面负责在每个需要的属性设置器中实现更改通知要将数据绑定到用户界面,否则更改将不会像您预期的那样进行同步。

And if you are implementing INotifyPropertyChanged, you are fully responsible to implement the change notification in every setter of the properties which needs to be data bound to the UI. Otherwise, the change will be not synchronized as you'd expect.

希望这样可以清除一些内容。

Hope this clears things up a little bit.

所以基本上你可以做到这一点,只要它是一个简单的CLR对象,很整洁,但完全意外 - 我做了一些WPF工作过去的几年,你永远不会停止学习新事物,对吧?

So basically you can do this, as long as its a plain CLR object. Pretty neat but totally unexpected - and i have done a bit of WPF work the past years. You never stop learning new things, right?

如Hasan Khan所建议的,这里是另一个链接到一个关于这个主题的有趣的文章

As suggested by Hasan Khan, here is another link to a pretty interesting article on this subject.


注意此功能仅在使用绑定时有效。如果您从代码更新值,则更改将不会被通知。 [...]

Note this only works when using binding. If you update the values from code, the change won't be notified. [...]

绑定时,WPF使用的权重更轻的PropertyInfo类。如果你明确地实现INotifyPropertyChanged,所有WPF需要做的就是调用PropertyInfo.GetValue方法来获取最新的值。这比获取所有描述符要少得多。描述符最终以4倍的财产信息类别的记忆的成本计算。 [...]

WPF uses the much lighter weight PropertyInfo class when binding. If you explicitly implement INotifyPropertyChanged, all WPF needs to do is call the PropertyInfo.GetValue method to get the latest value. That's quite a bit less work than getting all the descriptors. Descriptors end up costing in the order of 4x the memory of the property info classes. [...]

实施INotifyPropertyChanged可能是一个乏味的开发工作。但是,您需要根据WPF应用程序的运行时占用空间(内存和CPU)来衡量这项工作。 自己实现INPC将节省运行时CPU和内存

Implementing INotifyPropertyChanged can be a fair bit of tedious development work. However, you'll need to weigh that work against the runtime footprint (memory and CPU) of your WPF application. Implementing INPC yourself will save runtime CPU and memory.

这篇关于为什么绑定更新不实现INotifyPropertyChanged?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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