前端:
<StackPanel>
<Button Name="btnTest" Content="测试" Height="100" Click="btnTest_Click"/>
<TextBlock Name="tblkText" Text="init" TextAlignment="Center" Height="100"/>
</StackPanel>
Model:
public class Student : INotifyPropertyChanged
{
private string stuName;
public string StuName
{
get { return stuName; }
set
{
stuName = value;
if (PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("StuName"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
ViewModel:
public partial class MainWindow : Window
{
private Student myStu=new Student();
public MainWindow()
{
InitializeComponent();
Binding bd = new Binding();
bd.Source = myStu;
bd.Path = new PropertyPath("StuName");
BindingOperations.SetBinding(tblkText, TextBlock.TextProperty, bd);
}
private void btnTest_Click(object sender, RoutedEventArgs e)
{
myStu.StuName += "ab";
}
}