WPF基础入门
Class6-WPF通知
1、显示页面:
<Grid>
<StackPanel>
<TextBox Text="{Binding Name}"></TextBox>
<TextBox Text="{Binding Title}"></TextBox>
<Button Command="{Binding ShowCommand}" Content="Show"></Button>
</StackPanel>
</Grid>
页面cs文件:
InitializeComponent();
this.DataContext = new WPF_Learn.Model.model_csdn();
2、新建一个ViewModelBase.cs
文件
public class ViewModelBase : INotifyPropertyChanged
{
//实现接口
public event PropertyChangedEventHandler? PropertyChanged;
//public void OnPropertyChanged(string propertyName)
//{
// PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
//}
// 自动获取调用者的属性名,默认空,不需要传递参数
public void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
3、model文件:其中定义的Name
和Title
与xaml文件
中控件绑定一致文章来源:https://www.toymoban.com/news/detail-674857.html
//继承ViewModelBase
class model_csdn : ViewModelBase
{
public model_csdn()
{
Name = "Ini_name";
Title = "Ini_title";
ShowCommand = new MyCommamd(show);
}
public MyCommamd ShowCommand { get; set; }
public string name;
public string title;
public string Name
{
get { return name; }
set
{
name = value;
OnPropertyChanged();
}
}
public string Title
{
get { return title; }
set
{
title = value;
OnPropertyChanged();
}
}
public void show()
{
Name = "change name";
Title = "change title";
MessageBox.Show("change info");
}
}
运行效果:
文章来源地址https://www.toymoban.com/news/detail-674857.html
到了这里,关于WPF基础入门-Class6-WPF通知更改的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!