为什么绑定更新而不实现 INotifyPropertyChanged?
我创建了一个 ViewModel 并将其属性绑定到 UI 上的两个文本框。当我更改第一个文本框的值并将焦点移出文本框时,另一个文本框的值会发生变化,但我没有实现 INotifyPropertyChanged。这是如何运作的?
以下是 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
class ViewModel
{
public string Name { get; set; }
}
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?
Following is 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>
And following is my ViewModel
class ViewModel
{
public string Name { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我测试了一下,你是对的。现在我在网上搜索它,发现 这个。
基本上你可以做到这一点,只要它是一个简单的 CLR 对象,但完全出乎意料 - 我已经做了一些 WPF 工作。过去的几年里,你永远不会停止学习新事物,对吗?
正如哈桑·汗所建议的,这是另一个链接关于这个主题的一篇非常有趣的文章。
编辑:
更新此内容,因为我仍然时不时地从这里收到评论和点赞,所以它显然仍然相关,即使我自己已经有一段时间没有使用 WPF 了。但是,正如评论中提到的,请注意这可能会导致 内存泄漏。据说它也大量使用反射,这一点也已经提到过。
I tested it, you are right. Now i searched for it on the web, and found this.
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?
As suggested by Hasan Khan, here is another link to a pretty interesting article on this subject.
Edit:
Updating this, since i still get comments and upvotes now and then from here, so it clearly is still relevant, even thouh i myself have not worked with WPF for quite some time now. However, as mentioned in the comments, be aware that this may cause memory leaks. Its also supposedly heavy on the Reflection usage, which has been mentioned as well.
我刚刚发现这也适用于 WinForms,有点:/
但奇怪的是,这并没有禁用按钮:
这确实是:
I just found out that this also works in WinForms, kinda :/
Strangely though, this doesn't disable the button:
This does:
我可以解释为什么焦点更改时属性会更新:所有
Binding
都有一个UpdateSourceTrigger
属性,指示源属性何时更新。此默认值在每个DependencyProperty
上定义,并且TextBox.Text
属性设置为LostFocus
,这意味着该属性将被更新当控件失去焦点时。我相信 UrbanEsc 的答案解释了为什么该值会被更新
I can explain why the property is updated when focus changes: all
Binding
s have anUpdateSourceTrigger
property which indicates when the source property will be updated. The default value for this is defined on eachDependencyProperty
and for theTextBox.Text
property is set toLostFocus
, meaning that the property will be updated when the control loses focus.I believe UrbanEsc's answer explains why the value is updated at all