【发布时间】:2013-08-28 09:50:00
【问题描述】:
我正在尝试实现以下 INotifyPropertyChanged 扩展:
Automatically INotifyPropertyChanged(已接受答案) http://ingebrigtsen.info/2008/12/11/inotifypropertychanged-revisited/
但我无法弄清楚为什么我的 PropertyChanged EventHandler 保持为空。 :(
我做了一个非常简单的 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 实现来做示例,这可以按预期工作,但我无法通过这个扩展类实现它。
希望你能帮我弄清楚。 提前致谢。
【问题讨论】:
标签: c# wpf inotifypropertychanged