我正在尝试实现以下 INotifyPropertyChanged 扩展:
自动 INotifyPropertyChanged https://stackoverflow.com/questions/527602/automatically-inotifypropertychanged(接受的答案)http://ingebrigtsen.info/2008/12/11/inotifypropertychanged-revisited/ http://ingebrigtsen.info/2008/12/11/inotifypropertychanged-revisited/
但我不明白为什么我的 PropertyChanged 事件处理程序保持为空。 :(
我做了一个非常简单的 WPF 应用程序来测试它,这是我的 XAML 代码:
<StackPanel Orientation="Vertical">
<TextBox Text="{Binding Path=SelTabAccount.Test, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<TextBox Text="{Binding Path=SelTabAccount.TestRelated, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</StackPanel>
我的代码背后:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private TabAccount _selTabAccount;
public TabAccount SelTabAccount
{
get { return _selTabAccount; }
set
{
_selTabAccount = value;
PropertyChanged.Notify(() => this.SelTabAccount);
}
}
public MainWindow()
{
InitializeComponent();
SelTabAccount = new TabAccount()
{
Test = "qwer",
TestRelated = ""
};
}
}
public partial class TabAccount : INotifyPropertyChanged
{
private string _test;
public string Test
{
get { return _test; }
set
{
_test = value;
PropertyChanged.Notify(() => this.Test);
PropertyChanged.Notify(() => this.TestRelated);
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public partial class TabAccount
{
private string _testRelated;
public string TestRelated
{
get
{
_testRelated = Test + "_Related";
return _testRelated;
}
set
{
_testRelated = value;
PropertyChanged.Notify(() => this.TestRelated);
}
}
}
在后面的代码中,您将看到一个类(其部分用于随机测试)具有 2 个属性,它们应该通知属性更改,但没有任何反应。
NotificationExtension 是从顶部提供的链接复制并粘贴的内容,位于外部 cs 文件中。
我还尝试使用“正常”INotifyPropertyChanged 实现来执行示例,这按预期工作,但我无法使用此扩展类实现它。
希望你能帮我弄清楚。
提前致谢。