• WPF中 INotifyPropertyChanged


    在Windows Presentation Foundation (WPF)中,INotifyPropertyChanged 是一个核心接口,用于实现实体类与视图之间的数据双向绑定。当实体类的某个属性值发生变化时,通过实现此接口可以立即通知绑定到该属性的所有 UI 控件进行更新,ICommand主要针对的是关联到任何实现了 ICommand 接口的对象的方法

    在C#中,CallerMemberName是.NET框架提供的一个编译器特性(Compiler Feature),它允许你获取调用当前方法的成员名称,而无需硬编码该名称。这对于实现INotifyPropertyChanged接口特别有用,因为它可以减少手动输入属性名的工作量,提高代码的健壮性和可维护性。

    不管是ICommand还是INotifyPropertyChanged 都必须首先将ViewMode的实例设置为控件或整个界面的 DataContext如,this.DataContext = new MainViewModel();DataContext是UI层与数据逻辑层的桥梁。

    DataContext作为一个容器,提供了UI层和数据层之间的连接点。在MVVM(Model-View-ViewModel)架构模式中,通常将 ViewModel 设置为控件或整个界面的 DataContext,这样 UI 控件可以通过绑定直接访问 ViewModel 中的数据和命令。

    例如,在上面的INotifyPropertyChanged实现中,我们可以使用CallerMemberName特性来简化OnPropertyChanged方法的调用:

    1. 1using System.ComponentModel;
    2. 2using System.Runtime.CompilerServices;
    3. 3
    4. 4public class MyViewModel : INotifyPropertyChanged
    5. 5{
    6. 6 // ...
    7. 7
    8. 8 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
    9. 9 {
    10. 10 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    11. 11 }
    12. 12}
    13. 13
    14. 14public string MyProperty
    15. 15{
    16. 16 get { return myPropertyBackingField; }
    17. 17 set
    18. 18 {
    19. 19 if (myPropertyBackingField != value)
    20. 20 {
    21. 21 myPropertyBackingField = value;
    22. 22 OnPropertyChanged(); // 这里不再需要传入 "MyProperty"
    23. 23 }
    24. 24 }
    25. 25}

    在此版本中,当你调用OnPropertyChanged()时,编译器会自动填充propertyName参数,将其设为调用方法的成员名称,即"MyProperty"。这样,当MyProperty的值改变时,绑定系统能够准确地知道哪个属性发生了变化并作出相应的更新。

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Linq;
    5. using System.Runtime.CompilerServices;
    6. using System.Text;
    7. using System.Threading.Tasks;
    8. namespace WpfApplication1
    9. {
    10. public class MyNotifyProperyChanged : INotifyPropertyChanged //主要通过实现INotifyPropertyChanged接口,实现UI和属性绑定
    11. {
    12. public event PropertyChangedEventHandler PropertyChanged;
    13. public void OnPropertyChanged([CallerMemberName] string propertyname ="")//属性值变化调用事件,属性特性要调用的属性值给propertyname
    14. {
    15. PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyname));
    16. }
    17. }
    18. }
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. using System.Windows;
    7. namespace WpfApplication1
    8. {
    9. public class MainViewModel : MyNotifyProperyChanged
    10. {
    11. public MyCommand MyCommandOrder { get; set; }
    12. private string _name;
    13. public string Name
    14. {
    15. get { return _name; }
    16. set
    17. {
    18. _name = value;
    19. OnPropertyChanged();//调用基类事件
    20. }
    21. }
    22. private string _tittle;
    23. public string Tittle
    24. {
    25. get { return _tittle; }
    26. set { _tittle = value; OnPropertyChanged(); }
    27. }
    28. public MainViewModel()
    29. {
    30. MyCommandOrder = new MyCommand(ShowMessage);
    31. }
    32. public void ShowMessage()
    33. {
    34. Name = "点击了按钮";
    35. Tittle = "我是标题";
    36. MessageBox.Show(Name);
    37. }
    38. }
    39. }
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. using System.Windows;
    7. using System.Windows.Controls;
    8. using System.Windows.Data;
    9. using System.Windows.Documents;
    10. using System.Windows.Input;
    11. using System.Windows.Media;
    12. using System.Windows.Media.Imaging;
    13. using System.Windows.Navigation;
    14. using System.Windows.Shapes;
    15. namespace WpfApplication1
    16. {
    17. ///
    18. /// MainWindow.xaml 的交互逻辑
    19. ///
    20. public partial class MainWindow : Window
    21. {
    22. public MainWindow()
    23. {
    24. InitializeComponent();
    25. this.DataContext = new MainViewModel();
    26. }
    27. }
    28. }
    1. <Window x:Class="WpfApplication1.MainWindow"
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    6. xmlns:local="clr-namespace:WpfApplication1"
    7. mc:Ignorable="d"
    8. Title="MainWindow" Height="350" Width="525">
    9. <Grid>
    10. <StackPanel>
    11. <TextBox Text="{Binding Name}"> TextBox>
    12. <TextBox Text="{Binding Tittle}" >TextBox>
    13. <Button Content="按钮" Command="{Binding MyCommandOrder}">Button>
    14. StackPanel>
    15. Grid>
    16. Window>

  • 相关阅读:
    Stm32_标准库_9_TIM
    瑞吉外卖项目Day07-缓存优化
    docker安装skyWalking笔记
    Freertos学习第三天-ESP32基于Freertos任务共享全局变量
    二分图最大匹配(匈牙利算法)
    小程序 target 与 currentTarget(详细)
    百分点科技受邀参加“一带一路”国际合作高峰论坛
    华为云数据库 RDS for MySQL ,助力数字化企业安全发展
    Redis集群部署
    【HarmonyOS开发】配置开发工具DevEco Studio
  • 原文地址:https://blog.csdn.net/ABC13222880223/article/details/136662034