INotifyPropertyChanged - Event stays null

戏子无情 提交于 2019-12-06 04:29:34

问题


I am trying to implement the following INotifyPropertyChanged Extension:

Automatically INotifyPropertyChanged (accepted answer) http://ingebrigtsen.info/2008/12/11/inotifypropertychanged-revisited/

But I cant figure out why my PropertyChanged EventHandler stays null. :(

I did a very simple WPF Application to test it out, here is my XAML Code:

<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.

The NotificationExtension is a copy and paste from the links provided at top and is in a external cs file.

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.

Hope you can help me figure it out. Thanks in advance.


回答1:


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:

 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.




回答2:


You will have to set the DataContext of Window. You can do this in construnctor of window after initializecomponent.

this.DataContext = this;

this should do the trick.

Thanks



来源:https://stackoverflow.com/questions/18484581/inotifypropertychanged-event-stays-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!