为什么在 WPF 中使用 INotifyPropertyChanged 与绑定
本文关键字:INotifyPropertyChanged 绑定 WPF 为什么 | 更新日期: 2023-09-27 18:33:17
我注意到,我在互联网上找到的关于绑定的几乎每个示例都有一个类(绑定到另一个属性),它继承了 INotifyPropertyChanged 接口,并在类属性的 set 部分中使用方法。
我尝试从绑定示例中删除该部分,它的工作方式与该方法相同。
下面是示例。我已经更改了它,因此它将是一个双向绑定模式,并在消息框中显示更改的属性。
我这样做只是为了稍微玩一下绑定,但现在我真的不知道为什么要使用该接口
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="40"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
</Grid.ColumnDefinitions>
<Button Grid.Row="5" Grid.Column="5" Name="btnBinding" Click="btnBinding_Click" Width="100" Height="30">
<Grid HorizontalAlignment="Left" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>
<TextBox Name="txtBinding" Width="30" Height="25" HorizontalAlignment="Left"/>
<Label Grid.Column="1" Content="Bind"/>
</Grid>
</Button>
<Button Grid.Column="5" Grid.Row="6" Name="btnMessage" Click="btnMessage_Click" Content="MessageBox"/>
<Button Grid.Column="5" Grid.Row="4" Name="btnChangeproperty" Click="btnChangeproperty_Click" Content="Change Property"/>
</Grid>
</Window>
主.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Binding bind;
MyData mydata;
public MainWindow()
{
InitializeComponent();
}
private void btnBinding_Click(object sender, RoutedEventArgs e)
{
mydata = new MyData("T");
bind = new Binding("MyDataProperty")
{
Source = mydata,
Mode = BindingMode.TwoWay
};
txtBinding.SetBinding(TextBox.TextProperty, bind);
}
private void btnMessage_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(mydata.MyDataProperty);
}
private void btnChangeproperty_Click(object sender, RoutedEventArgs e)
{
mydata.MyDataProperty = "New Binding";
}
}
}
我的数据类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace WpfApplication1
{
public class MyData
{
private string myDataProperty;
public MyData() { }
public MyData(DateTime dateTime)
{
myDataProperty = "Last bound time was " + dateTime.ToLongTimeString();
}
public MyData(string teste)
{
myDataProperty = teste;
}
public String MyDataProperty
{
get { return myDataProperty; }
set
{
myDataProperty = value;
OnPropertyChanged("MyDataProperty");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string info)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(info));
}
}
}
}
如果您只打算使用绑定写入属性(如您所发现的那样),则不需要INotifyPropertyChanged
,但您确实需要它,以便您可以判断其他人写入了该属性并相应地更新显示的值。
若要了解我在说什么,请在窗口中添加一个按钮,单击该按钮时直接更改绑定属性的值(而不是绑定到该属性的 UI 元素的相应属性)。使用 INotifyPropertyChanged
,当您单击按钮时,您将看到 UI 自行更新为新值;没有它,UI 仍将显示"旧"值。
从这里的讨论中,我认为您错过了实现
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(“Propety Name”))
实现后,您可以看到 UI 正在自动更新。您可以在我的博客上查看有关 MSDN 的详细信息或简要版本。