学习后尝试练习一下发现 Test的事件PropertyChanged始终为空。请问一下我写的代码哪里需要修改呢,现在的问题是什么原因。我感觉是自己binding 的path没写对,或者是datacontext没对。因为这两个我不知道该怎样写,该写在哪里?
<Window x:Class="WpfApplication3.MainWindow"
Name="winM"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock x:Name="txt2" Background="Yellow" Text="{Binding Path = this.A , Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap" Margin="33,127,32,92" />
<Button Content="Button" HorizontalAlignment="Left" Margin="140,263,0,0" VerticalAlignment="Top" Width="115" Height="47" Click="Button_Click"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="334,263,0,0" VerticalAlignment="Top" Width="115" Height="43" Click="Button_Click_1"/>
</Grid>
</Window>
//cs代码
namespace WpfApplication3
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
test = new Test();
this.txt2.DataContext = test;
}
public Test test;
private void Button_Click(object sender, RoutedEventArgs e)
{
test.A = "1111111111";
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
test.A = "22222222222";
}
}
public class Test : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string a;
public string A
{
get { return a; }
set
{
a = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("A"));
}
}
}
}
}