为什么OnPropertyChanged的代码无法正常工作的背后? [英] Why does OnPropertyChanged not work in Code Behind?
问题描述
我试图通过将视图模型的模型到代码后面,结合DataContext的本,以简化一些代码,但它似乎不同的工作,下面的例子:
为什么点击按钮时,文本块势必消息不改变,即使OnPropertyChanged(信息)被称为?
XAML:
<窗口x:类= TestSimple223.Window1
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/ 2006 / XAML
标题=窗口1HEIGHT =300WIDTH =300>
< StackPanel中的HorizontalAlignment =左>
<按钮内容=按钮
点击=的button1_Click/>
< TextBlock的
文本={绑定路径=消息,模式=双向}/>
< TextBlock的
X:名称=消息2/>
< / StackPanel的>
< /窗GT;
代码背后:
使用System.Windows;
使用System.ComponentModel;
命名空间TestSimple223
{
公共部分类窗口1:窗口
{
#地区ViewModelProperty:消息
私人字符串_message;
公共字符串消息
{
得到
{
返回_message;
}
组
{
_message =价值;
OnPropertyChanged(信息);
}
}
#endregion
公共窗口1()
{
的InitializeComponent();
的DataContext =这一点;
消息=原始信息;
Message2.Text =原MESSAGE2
}
私人无效的button1_Click(对象发件人,RoutedEventArgs E)
{
消息=按钮被点击,留言改变了;
Message2.Text =按钮被点击,MESSAGE2改变;
}
#地区的inotify
公共事件PropertyChangedEventHandler的PropertyChanged;
保护无效OnPropertyChanged(字符串propertyName的)
{
如果(的PropertyChanged!= NULL)
的PropertyChanged(这一点,新PropertyChangedEventArgs(propertyName的));
}
#endregion
}
}
您还没有标记类为可用的属性更改通知。更改标题为
公共部分类窗口1:窗口,INotifyPropertyChanged的
只是因为你实现的方法并不意味着WPF知道,一类支持更改通知 - 你需要通过INotifyPropertyChanged的标记它告诉它。这种方式中,结合机构能够确定类作为潜在的更新目标
I'm trying to simplify some code by putting the ViewModel models into the code behind and binding the DataContext as "this", but it seems to work differently, in the following example:
Why is it when the button is clicked, the TextBlock bound to "Message" does not change, even though OnPropertyChanged("Message") is called?
XAML:
<Window x:Class="TestSimple223.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel HorizontalAlignment="Left">
<Button Content="Button"
Click="button1_Click" />
<TextBlock
Text="{Binding Path=Message, Mode=TwoWay}"/>
<TextBlock
x:Name="Message2"/>
</StackPanel>
</Window>
Code Behind:
using System.Windows;
using System.ComponentModel;
namespace TestSimple223
{
public partial class Window1 : Window
{
#region ViewModelProperty: Message
private string _message;
public string Message
{
get
{
return _message;
}
set
{
_message = value;
OnPropertyChanged("Message");
}
}
#endregion
public Window1()
{
InitializeComponent();
DataContext = this;
Message = "original message";
Message2.Text = "original message2";
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Message = "button was clicked, message changed";
Message2.Text = "button was click, message2 changed";
}
#region INotify
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}
You haven't marked your class as being available for property change notification. Change the heading to
public partial class Window1 : Window, INotifyPropertyChanged
Just because you implement the methods doesn't mean that WPF knows that a class supports change notification - you need to tell it by marking it with INotifyPropertyChanged. This way, the binding mechanism can identify your class as a potential update target.
这篇关于为什么OnPropertyChanged的代码无法正常工作的背后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!