模板网站下载,潍坊学校网站建设,体育局网站建设方案,备案个人网站在Windows Presentation Foundation (WPF)中#xff0c;INotifyPropertyChanged 是一个核心接口#xff0c;用于实现实体类与视图之间的数据双向绑定。当实体类的某个属性值发生变化时#xff0c;通过实现此接口可以立即通知绑定到该属性的所有 UI 控件进行更新#xff0c;…在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层和数据层之间的连接点。在MVVMModel-View-ViewModel架构模式中通常将 ViewModel 设置为控件或整个界面的 DataContext这样 UI 控件可以通过绑定直接访问 ViewModel 中的数据和命令。
例如在上面的INotifyPropertyChanged实现中我们可以使用CallerMemberName特性来简化OnPropertyChanged方法的调用
1using System.ComponentModel;
2using System.Runtime.CompilerServices;
3
4public class MyViewModel : INotifyPropertyChanged
5{
6 // ...
7
8 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName )
9 {
10 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
11 }
12}
13
14public string MyProperty
15{
16 get { return myPropertyBackingField; }
17 set
18 {
19 if (myPropertyBackingField ! value)
20 {
21 myPropertyBackingField value;
22 OnPropertyChanged(); // 这里不再需要传入 MyProperty
23 }
24 }
25}
在此版本中当你调用OnPropertyChanged()时编译器会自动填充propertyName参数将其设为调用方法的成员名称即MyProperty。这样当MyProperty的值改变时绑定系统能够准确地知道哪个属性发生了变化并作出相应的更新。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;namespace WpfApplication1
{public class MyNotifyProperyChanged : INotifyPropertyChanged //主要通过实现INotifyPropertyChanged接口实现UI和属性绑定{public event PropertyChangedEventHandler PropertyChanged;public void OnPropertyChanged([CallerMemberName] string propertyname )//属性值变化调用事件属性特性要调用的属性值给propertyname{PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyname));}}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;namespace WpfApplication1
{public class MainViewModel : MyNotifyProperyChanged{public MyCommand MyCommandOrder { get; set; }private string _name;public string Name{get { return _name; }set{_name value;OnPropertyChanged();//调用基类事件}}private string _tittle;public string Tittle{get { return _tittle; }set { _tittle value; OnPropertyChanged(); }}public MainViewModel(){MyCommandOrder new MyCommand(ShowMessage);}public void ShowMessage(){Name 点击了按钮;Tittle 我是标题;MessageBox.Show(Name);}}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WpfApplication1
{/// summary/// MainWindow.xaml 的交互逻辑/// /summarypublic partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext new MainViewModel();}}
}Window x:ClassWpfApplication1.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WpfApplication1mc:IgnorabledTitleMainWindow Height350 Width525GridStackPanelTextBox Text{Binding Name} /TextBoxTextBox Text{Binding Tittle} /TextBoxButton Content按钮 Command{Binding MyCommandOrder}/Button !-- Command命令绑定绑定的也是一个属性需要指定数据源实现ICommand接口 --/StackPanel/Grid
/Window