使用INotifyPropertyChanged的WPF绑定不会更新

编程入门 行业动态 更新时间:2024-10-20 16:27:39
本文介绍了使用INotifyPropertyChanged的WPF绑定不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

更新绑定属性时,更新WPF UI时似乎出现严重问题.这是我的视图模型类定义:

I appear to be having serious problems getting my WPF UI to update when I update when I update the property it is bound to. Here is my view model class definition:

namespace WpfModel { class AppModel : INotifyPropertyChanged { private int _count = 7; public int Count { get { return _count; } set { _count = value; OnPropertyChanged("Count"); } } public void Increment() { Count++; } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string prop) { PropertyChangedEventHandler handler = this.PropertyChanged; if (handler != null) { var e = new PropertyChangedEventArgs("prop"); handler(this, e); } } }; }

这在以下XAML中绑定到我的简单UI:

This is bound to my simple UI in the following XAML:

<Window x:Class="WpfModel.MainWindow" xmlns="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/winfx/2006/xaml" xmlns:vm="clr-namespace:WpfModel" Title="WPF Data Model Demo" Height="128" Width="284" > <Window.DataContext> <vm:AppModel /> </Window.DataContext> <Grid Height="Auto" Width="Auto"> <Button Margin="0,0,12,12" Name="IncButton" Height="23" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="75" Click="IncButton_Click" Content="Increment" /> <Label Content="Count Variable:" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label1" VerticalAlignment="Top" /> <Label Height="28" Margin="116,12,0,0" Name="CountLabel" VerticalAlignment="Top" HorizontalAlignment="Left" Width="40" Content="{Binding Path=Count}" /> </Grid> </Window>

该应用程序的定义如下:

The application is defined like follows:

namespace WpfModel { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { private AppModel _model = new AppModel(); public MainWindow() { InitializeComponent(); //_model = new AppModel(); } private void IncButton_Click(object sender, RoutedEventArgs e) { } } }

启动应用程序时,所有内容都是桃红色的,标签甚至最初会显示模型的Count属性中的任何值.调用增量可以正确地更新模型,并直接增加Count属性(如代码中所示)也可以正常工作.

When the app starts up, everything is peachy, the label even initially displays whatever value is in the Count property of the model. Calling increment updates the model properly, and directly incrementing the Count property as shown in the code also works just fine.

这里的问题是,PropertyChanged事件处理程序似乎从未添加到其中.我可以单步调试器中的代码,并查看属性更新中的值,甚至可以看到对OnPropertyChanged的调用,但PropertyChanged处理程序本身始终为null.

The problem here, is that the PropertyChanged event handler never seems to get added to. I can step through the code in the debugger and see the value in the property updating, and I can see the call to OnPropertyChanged even, but the PropertyChanged handler itself is always null.

也许我的装订有问题?

我正在使用MSVC 2010 Express.

I am using MSVC 2010 Express.

推荐答案

问题是您拥有作为实例变量_model的WpfModel 不是与用作Window的实例相同DataContext.因此,它不受任何约束,并且其PropertyChanged始终为null.

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.

更改XAML以将数据上下文设置为此:

Change your XAML for setting the datacontext to this:

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

摆脱后面的代码中声明的实例变量,并修复OnPropertyChanged实现(使用参数而不是文字字符串"prop"),即可正常工作.

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.

更多推荐

使用INotifyPropertyChanged的WPF绑定不会更新

本文发布于:2023-11-11 23:23:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579822.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   INotifyPropertyChanged   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!