为什么ToString无法与数据绑定一起使用 [英] Why doesn't ToString work with Data Binding
问题描述
按照此示例(示例2)数据绑定有效,并且UI根据 ArtistName
的值进行更新。
Following this example (Example 2) Data Binding works and the UI is updated based on the value of ArtistName
.
如果xaml绑定是这样写的:
If xaml binding is written like this:
<Label Grid.Column="1" Grid.Row="1" Content="{Binding}" />
而不是
<Label Grid.Column="1" Grid.Row="1" Content="{Binding ArtistName}" />
然后在 SongViewModel
上调用ToString,然后即使我这样重写它:
then ToString is called on SongViewModel
, and even if I override it like this:
public override String ToString()
{
return Song.ArtistName;
}
该值不会通过DataBinding更新。其余代码与链接中给出的示例相同。
the value is not updated through DataBinding. Rest of the code is same as in the example given in the link.
在这种情况下为什么值不变?
Why doesn't value change in this case?
推荐答案
WPF绑定无法正常工作。
WPF Binding doesn't work that way.
如果您 {Binding}
,则WPF刷新该绑定的唯一方法是重置 DataContext
。该绑定没有要遵循的 PropertyPath
,因此不能调用 INotifyPropertyChanged
来刷新这种绑定。
If you {Binding}
then the only way for WPF to refresh that binding is that you reset the DataContext
. That Binding has no PropertyPath
to follow therefore INotifyPropertyChanged
cannot be called to refresh that kind of Binding.
这篇关于为什么ToString无法与数据绑定一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!