使用 INotifyPropertyChanged

using of INotifyPropertyChanged

谁能解释一下为什么在 wpf 中使用绑定时需要使用 INotifyPropertyChanged 的​​实现? 我可以在不实现此接口的情况下绑定属性吗? 例如我有代码

public class StudentData : INotifyPropertyChanged
{
    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    #endregion

    void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    string _firstName = null;
    public string StudentFirstName
    {
        get
        {
            return _firstName;
        }
        set
        {
            _firstName = value;

            OnPropertyChanged("StudentFirstName");
        }
    }
}

并绑定在 .xaml

<TextBox Text="{Binding Path=StudentFirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
   Grid.Row="1"
   Grid.Column="2"
   VerticalAlignment="Center" />

此代码来自 .xaml.cs

StudentData _studentData = new StudentData { StudentFirstName = "John", StudentGradePointAverage = 3.5};

public MainWindow()
{
    InitializeComponent();

    this.DataContext = _studentData;
}

为什么我们需要在这种情况下使用 INotifyPropertyChanged? 这不是我的代码。

如果您希望 wpf 表单在 属性 通过代码更改时自动更新,则需要 INotifyPropertyChanged。此外,一些控制器可能想知道是否进行了编辑,例如 enable/disable 保存按钮。您也可能在不同的视图上显示相同的 属性;在这种情况下,INotifyPropertyChanged 有助于在您编辑 属性.

时立即更新另一个视图

如果您认为您的表单在没有 INotifyPropertyChanged 的情况下表现良好,那么您可以放弃它。

请注意,即使没有 INotifyPropertyChanged,绑定也能正常工作。参见:Why does the binding update without implementing INotifyPropertyChanged?


我会实现这样的属性。在极少数情况下,它可以帮助避免无休止的循环更新。顺便说一句,它更有效率。

 private string _firstName;
 public string StudentFirstName
 {
     get { return _firstName; }
     set
     {
         if (value != _firstName) {
             _firstName = value;
             OnPropertyChanged("StudentFirstName");
         }
     }
 }

从 C#6.0 (VS 2015) 开始,您可以这样实现 OnPropertyChanged

private void OnPropertyChanged(string propertyName)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

当您绑定到 StudentData 的 属性 时,例如 StudentFirstName 然后绑定 class 测试查看 StudentData 实例是否提供 INotifyPropertyChanged 接口。如果是这样,那么它将挂接到 PropertyChanged 事件。当事件触发并且由于 StudentFirstName 属性 而触发时,它知道它需要再次恢复源值,因为它已更改。这就是绑定能够监视源中的更改并将它们反映在用户界面中的方式。

如果您不提供 INotifyPropertyChanged 接口,则绑定不知道源值何时更改。在这种情况下,当 属性 更改时,用户界面将不会更新。您只会看到首次使用绑定时定义的初始值。

它确实需要实现才能使绑定工作,但这并不意味着您总是必须自己做。还有其他选项,例如 Castle Dynamic Proxy (which wraps your classes in a proxy and injects INPC into all virtual properties) and Fody (which adds it to the IL in a post-processing step). It's also possible to implement yourself while at the same time reducing code bloat, as demonstrated in .