WPF Binding with INotifyPropertyChanged does not update

筅森魡賤 提交于 2019-12-05 05:08:55

The issue is that the WpfModel you have as an instance variable, _model, is not the same instance that's being used as the Window's DataContext. Thus, it is not bound to by anything, and its PropertyChanged will always be null.

Change your XAML for setting the datacontext to this:

<Window.DataContext>
            <vm:AppModel x:Name="_model" />
</Window.DataContext>

Get rid of the instance variable declared in the code behind, and fix the OnPropertyChanged implementation (use the parameter instead of the literal string "prop"), and it works.

this line:

var e = new PropertyChangedEventArgs("prop");

should be:

var e = new PropertyChangedEventArgs(prop);

You are passing the string "prop" so the bound values listening for "Count" will not be triggered.

**EDIT ** Based on your other comments (Answer?) I thought I had better update this with a fix - you could use the answer already given of course but I dont want to copy someone else :).

Add this to your click handler:

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