我正在尝试在 xaml 中为我的 TextBlock 指定数据变量:
<TextBlock Name="Test11" Text="{Binding Path=Test}"></TextBlock>
我正在使用 OnPropertyChanged:
public partial class MainWindow : Window, INotifyPropertyChanged
private string _test;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
public string Test
{
get
{
return _test;
}
set
{
_test = value;
OnPropertyChanged("Test");
}
}
并尝试在 MainWindow 构造函数中设置值:
public MainWindow()
{
InitializeComponent();
Test = "teeest";
}
但是 Textblock.Text 没有更新......我做错了什么?