wpf INotifyPropertyChanged - 事件保持为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18484581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
INotifyPropertyChanged - Event stays null
提问by Rand Random
I am trying to implement the following INotifyPropertyChanged Extension:
我正在尝试实现以下 INotifyPropertyChanged 扩展:
Automatically INotifyPropertyChanged(accepted answer) http://ingebrigtsen.info/2008/12/11/inotifypropertychanged-revisited/
自动 INotifyPropertyChanged(接受的答案) http://ingebrigtsen.info/2008/12/11/inotifypropertychanged-revisited/
But I cant figure out why my PropertyChanged EventHandler stays null. :(
但我无法弄清楚为什么我的 PropertyChanged EventHandler 保持为空。:(
I did a very simple WPF Application to test it out, here is my XAML Code:
我做了一个非常简单的 WPF 应用程序来测试它,这是我的 XAML 代码:
<StackPanel Orientation="Vertical">
<TextBox Text="{Binding Path=SelTabAccount.Test, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<TextBox Text="{Binding Path=SelTabAccount.TestRelated, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</StackPanel>
And my code behind:
而我背后的代码:
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);
}
}
}
In the code behind you will see one class (its partial for just random testing) with 2 properties that should notify a property change but nothing happens.
在后面的代码中,您将看到一个具有 2 个属性的类(它只是随机测试的部分),这些属性应该通知属性更改但没有任何反应。
The NotificationExtension is a copy and paste from the links provided at top and is in a external cs file.
NotificationExtension 是从顶部提供的链接复制和粘贴的,位于外部 cs 文件中。
I have also tried to do the sample with the "normal" INotifyPropertyChanged implementation and this works as expected but I cant make it happen with this extension class.
我还尝试使用“正常” INotifyPropertyChanged 实现来做示例,这按预期工作,但我无法使用此扩展类实现。
Hope you can help me figure it out. Thanks in advance.
希望你能帮我弄清楚。提前致谢。
采纳答案by JSJ
Binding will work only when you provide some datasource to the visual objects. If you don't provide any datasource and want to dig into the properties the binding will not work.
只有当您向可视对象提供一些数据源时,绑定才会起作用。如果您不提供任何数据源并想深入了解属性,则绑定将不起作用。
Under your MainWindow Constructor, set the DataContext property of the Window to a datasource. For example:
在 MainWindow Constructor 下,将 Window 的 DataContext 属性设置为数据源。例如:
public MainWindow()
{
InitializeComponent();
// your property setups
this.DataContext = this;
}
Essentially this makes the MainWindow properties available for binding to the visual tree of MainWindow items.
本质上,这使得 MainWindow 属性可用于绑定到 MainWindow 项的可视化树。
回答by Nitin
You will have to set the DataContext of Window. You can do this in construnctor of window after initializecomponent.
您必须设置 Window 的 DataContext。您可以在初始化组件之后在窗口的构造函数中执行此操作。
this.DataContext = this;
this should do the trick.
这应该可以解决问题。
Thanks
谢谢

![wpf SecureString 到 Byte[] C#](/res/img/loading.gif)