gpt4 book ai didi

c# - 绑定(bind)在没有 INotifyPropertyChanged 的​​情况下工作,为什么?

转载 作者:太空狗 更新时间:2023-10-29 23:12:56 26 4
gpt4 key购买 nike

这是我们通常的做法:

public class ViewModel : INotifyPropertyChanged
{
string _test;
public string Test
{
get { return _test; }
set
{
_test = value;
OnPropertyChanged();
}
}

public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string property = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}

现在我们的属性可以被 View 中的多个元素使用,例如:

<TextBox Text="{Binding Test, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Text="{Binding Test}" />

更改 TextBox 中的值将更新 TextBlock 的内容。我们还可以在 View 模型中设置值, View 会自动更新它。

如果我们这样写 View 模型

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

然后 View 仍然有效(例如,更改 TextBox 中的值将更新 TextBlock)。当然,不可能再从 View 模型中轻松更新 Test 值(不再有事件发生)。但我的问题是关于 View :为什么 View 能够工作?它是在后台构造更多东西还是检查某些东西的逻辑?

最佳答案

[...] 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.

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.

参见:Data binding without INotifyPropertyChanged

关于c# - 绑定(bind)在没有 INotifyPropertyChanged 的​​情况下工作,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40972605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com