INotifyPropertyChanged
实现
INotifyPropertyChanged
会向客户端发出某一属性值已更改的通知。当元素属性值改变时,会通知后台model
前台代码不变,我们让后台Students Model
实现INotifyPropertyChanged
接口。
通过DataContext
实现数据数据项的变更通知。添加一个ViewModel
类,实现INotifyPropertyChanged
接口
前台xmal
<Window x:Class="WpfAppTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel Height="295" Width="427" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0" Name="stackPane1" >
<TextBlock Height="23" Name="txtBlock1" Text="学员编号"></TextBlock>
<TextBox Height="23" Name="txtStudentId" Width="301" HorizontalAlignment="Left"></TextBox>
<TextBlock Height="23" Name="txtBlock2" Text="学员列表"></TextBlock>
<ListBox Height="156" Name="lbStudent" Width="305" HorizontalAlignment="Left" ItemsSource="{Binding StudentList,Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Name="stackPanel2" Orientation="Horizontal">
<TextBlock Text="{Binding Id,Mode=TwoWay}" Margin="5" Background="Beige"></TextBlock>
<TextBlock Text="{Binding Name,Mode=TwoWay}" Margin="5"></TextBlock>
<TextBlock Text="{Binding Age,Mode=TwoWay}" Margin="5"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Button" Height="23" Name="button1" Width="75" HorizontalAlignment="Left" Click="Button1_Click"></Button>
</StackPanel>
</Grid>
</Window>
后台cs
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
/// <summary>
/// - 字段或属性实现INotifyPropertyChanged接口,实现了该接口,只要字段或属性的发生了改变,就会提供通知机制
/// - WPF 提供 ObservableCollection<T> 类,它是实现 INotifyCollectionChanged 接口的数据集合的内置实现,以便集合中的插入或删除操作可以自动更新 UI
/// </summary>
namespace WpfAppTest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
ViewModel viewModel = new ViewModel();
public MainWindow()
{
InitializeComponent();
viewModel.StudentList = new ObservableCollection<Students>
{
new Students(){Id=1,Age=11,Name="Tom"},
new Students(){Id=2,Age=12,Name="Darren"},
new Students(){Id=3,Age=13,Name="Jacky"},
new Students(){Id=4,Age=14,Name="Andy"}
};
this.lbStudent.DataContext = viewModel;
this.txtStudentId.SetBinding(TextBox.TextProperty, new Binding("SelectedItem.Id") { Source = lbStudent });
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
viewModel.StudentList[1] = new Students() { Id = 4, Age = 14, Name = "这是一个集合改变" };
viewModel.StudentList = new ObservableCollection<Students>() {
new Students(){ Id=1, Age=11, Name="这是改变后的集合"},
new Students(){ Id=2, Age=12, Name="这是改变后的集合"},
new Students(){ Id=3, Age=13, Name="这是改变后的集合"},
new Students(){ Id=4, Age=14, Name="这是改变后的集合"}
};
viewModel.StudentList[2].Name = "这是一个属性改变";
}
public class Students:INotifyPropertyChanged
{
public int Id { get; set; }
string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name");
}
}
public int Age { get; set; }
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class ViewModel : INotifyPropertyChanged
{
private ObservableCollection<Students> studentList;
public ObservableCollection<Students> StudentList {
get { return studentList; }
set
{
if (this.studentList != value)
{
this.studentList = value;
OnPropertyChanged("StudentList");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
}