WPF基础入门-Class6-WPF通知更改

WPF基础入门

Class6-WPF通知

1、显示页面:

html 复制代码
    <Grid>
        <StackPanel>
            <TextBox Text="{Binding Name}"></TextBox>
            <TextBox Text="{Binding Title}"></TextBox>
            <Button Command="{Binding ShowCommand}" Content="Show"></Button>
        </StackPanel>
    </Grid>

页面cs文件:

cs 复制代码
InitializeComponent();
this.DataContext = new WPF_Learn.Model.model_csdn();

2、新建一个ViewModelBase.cs文件

csharp 复制代码
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文件:其中定义的NameTitlexaml文件中控件绑定一致

csharp 复制代码
//继承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");
        }
    }

运行效果:

相关推荐
微小冷13 分钟前
WPF中ListView控件详解
c#·wpf·数据绑定·listview·bingding
专注VB编程开发20年1 天前
WPF,Winform,HTML5网页,哪个UI开发速度最快?
大数据·c#·wpf
YANQ6621 天前
3.1 WPF画折线图、直方图、饼状图
wpf
上元星如雨1 天前
WPF demo:全屏加载界面
wpf
深漂阿碉1 天前
WPF自定义日历选择控件
wpf
六点的晨曦1 天前
WPF的三轴机械手控件动画
wpf
军训猫猫头2 天前
5.浏览本地文件获取路径与文件名称 C#例子 WPF例子
开发语言·c#·wpf
步、步、为营2 天前
.NET + WPF框架开发聊天、网盘、信息发布、视频播放功能
.net·wpf·音视频
code bean3 天前
【WPF实战】MVVM中如何从数据模型反查自定义控件实例(ImageView + Halcon)
wpf
lph19723 天前
ValueConverter转换器WPF
wpf