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

运行效果:

相关推荐
狮恒22 分钟前
OpenHarmony Flutter 分布式任务调度:跨设备资源协同与负载均衡方案
分布式·flutter·wpf·openharmony
嗝o゚1 小时前
Flutter适配鸿蒙多屏异构UI开发实战
flutter·开源·wpf·harmonyos
小白|1 小时前
Flutter 与 OpenHarmony 深度集成:实现跨设备传感器数据协同监测系统
flutter·wpf
棉晗榜2 小时前
WPF输入框里面加文本提示
wpf
嗝o゚2 小时前
鸿蒙跨端协同与Flutter结合的远程办公轻应用开发
flutter·华为·wpf
豫狮恒2 小时前
OpenHarmony Flutter 分布式权限管理:跨设备可信访问与权限协同方案
分布式·flutter·wpf·openharmony
小白|2 小时前
Flutter 与 OpenHarmony 深度整合:构建跨设备统一剪贴板同步系统
flutter·wpf
He BianGu2 小时前
【笔记】在WPF中如何使用ContentPresenter 与 Generic.xaml 设置数据默认 DataTemplate
windows·笔记·wpf
小白|3 小时前
Flutter 与 OpenHarmony 深度融合:实现分布式文件共享与跨设备协同编辑系统
分布式·flutter·wpf
豫狮恒3 小时前
OpenHarmony Flutter 分布式数据持久化:跨设备数据一致性与同步方案
分布式·安全·flutter·wpf·openharmony