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");
        }
    }

运行效果:

相关推荐
sdff113961 小时前
【HarmonyOS】鸿蒙Flutter跨设备流转技术实战指南
flutter·wpf·harmonyos
wzqllwy13 小时前
java实战-分布式事务
wpf
YrqnxehxDo2 天前
相场模拟——合金,金属凝固模型,各向异性枝晶生长karma 合金凝固模型,选区激光熔融,激光增...
wpf
竟未曾年少轻狂3 天前
Spring Boot 项目集成 Redis
java·spring boot·redis·缓存·消息队列·wpf·redis集群
清风~徐~来3 天前
【视频点播系统】Redis-SDK 介绍及使用
数据库·redis·wpf
棉晗榜3 天前
WPF DataGrid鼠标滚不动,划不动解决
wpf
baivfhpwxf20233 天前
wpf自适应布局
wpf
艺杯羹3 天前
Git版本控制深度复盘:从入门到精通的完整指南
git·wpf·版本控制·git学习·git复盘
玄〤4 天前
RabbitMQ 入门篇总结(黑马微服务课day10)(包含黑马商城业务改造)
java·笔记·分布式·spring cloud·微服务·rabbitmq·wpf