参考文档:WPF MVVM框架中的INotifyPropertyChanged - 知乎 (zhihu.com)
INotifyPropertyChanged 接口用于通知视图或 ViewModel 绑定哪个属性无关紧要;它已更新。
让我们举个例子来理解这个接口。以一个 WPF 窗口为例,其中共有三个字段:名字、姓氏和全名。在这里,名字和姓氏文本框是可编辑的。因此,根据名字和姓氏的变化,我们必须自动更新全名。
使窗户设计图
WPF 窗口的 XAML 代码如下
<Window x:Class="MVVM_INotifyPropertyChanged.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"> <Grid Width="400" Height="Auto" HorizontalAlignment="Center" VerticalAlignment="Stretch" Margin="20"> <Grid.RowDefinitions> <RowDefinition Height="40" /> <RowDefinition Height="40" /> <RowDefinition Height="40" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="90" /> <ColumnDefinition/> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Content="First Name : "></Label> <Label Grid.Row="1" Grid.Column="0" Content="Last Name : "></Label> <Label Grid.Row="2" Grid.Column="0" Content="Full Name : "></Label> <TextBox Grid.Row="0" Grid.Column="1"></TextBox> <TextBox Grid.Row="1" Grid.Column="1"></TextBox> <TextBox Grid.Row="2" Grid.Column="1"></TextBox> </Grid> </Window>View Code
现在,我们创建一个模型,它包含一个类调用人,它有3个属性“FirstName”,“LastName”,“FullName”。
public class Person { private string _fisrtname; public string FirstName { get { return _fisrtname; } set { _fisrtname = value; } } private string _lastname; public string LastName { get { return _lastname; } set { _lastname = value; } } private string _fullname; public string FullName { get { return _fisrtname +" "+_lastname; ; } set { _fullname = value; } } public Person() { _fisrtname = "Nirav"; _lastname = "Daraniya"; } }View Code
标签:LYT,fisrtname,string,MVVM,lastname,WPF,public,INotifyPropertyChanged From: https://www.cnblogs.com/ViolinHuang/p/17448588.html