某些语法声明时INotifyPropertyChanged的不火

某些语法声明时INotifyPropertyChanged的不火

本文介绍了在某些语法声明时INotifyPropertyChanged的不火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然在a看似无关的问题,我已经打了一些意外的绑定行为。有

While investigating on a seemingly unrelated issue, I've hit some unexpected binding behaviour. Having

class StringRecord : INotifyPropertyChanged
{
    public string Key {get; set; }    // real INPC implementation is omitted
    public string Value { get; set; } // real INPC implementation is omitted
    ...
}

class Container
{
    public ObservableKeyedCollection<string, StringRecord> Params { get; set; }
    ...
{

现在,当一个TextBox绑定到明显的方式收集项目之一

Now, when a TextBox is bound to one of the collection items in obvious way

<TextBox Text="{Binding Params[APN_HOST].Value}" />

在StringRecord的实例的PropertyChanged事件不会开火编辑文本。但是,改写为

the PropertyChanged event of the StringRecord's instance doesn't fire upon editing the text. But, rewriting it as

<TextBox DataContext="{Binding Params[APN_HOST]}" Text="{Binding Value}" />

做的奇迹,以及事件开始正确触发。

makes the miracle, and the event begins to fire correctly.

为什么?

推荐答案

在第二XAML样品观察StringRecord其中实现INotifyPropertyChanged并且因此通知更改对象的结合

In the second xaml sample the binding is observing a StringRecord which implements INotifyPropertyChanged and thus is notified of changes to the object.

在第一XAML样品目前尚不清楚你所绑定。

In the first xaml sample it isn't clear what you are binding to.

如果您设置的DataContext为集装箱被观察对象没有实现INotifyPropertyChanged接口的绑定。因为路径仍然是正确的Value属性仍然可以读,但你在通知错过了。

If you set the DataContext to Container the binding is observing an object that doesn't implement the INotifyPropertyChanged interface. Because the path is still correct the Value property can still be read but you are missing out on the notifications.

这篇关于在某些语法声明时INotifyPropertyChanged的不火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 08:35