c# - WPF INotifyPropertyChanged 是如何工作的?

标签 c# wpf

这是在 WPF/C# 中使用绑定(bind)的典型 INotifyPropertyChanged 实现。

namespace notifications.ViewModel
{
    class MainViewModel : INotifyPropertyChanged
    {
        public const string NamePropertyName = "CheckBoxState";
        private bool _checkboxstate = true;

        public bool CheckBoxState
        {
            get { return _checkboxstate; }
            set
            {
                if (_checkboxstate == value) return;
                _checkboxstate = value;
                RaisePropertyChanged(NamePropertyName);
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

我还有一个绑定(bind)到 CheckBoxState 的 XAML 代码. enter image description here

<Grid>
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <CheckBox Content="Click Me" IsChecked="{Binding Path=CheckBoxState, Mode=TwoWay}" />
        <TextBlock Text="{Binding Path=CheckBoxState, Mode=TwoWay}" />
    </StackPanel>
</Grid>

这是连接 DataContext 和模型的 MainWindow.xaml.cs。

public partial class MainWindow : Window
{
    notifications.ViewModel.MainViewModel model = new notifications.ViewModel.MainViewModel();

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = model;
    }
}

当用户设置复选框时,我认为会发生如下情况:IsChecked变为真,并带有 "{Binding Path=CheckBoxState, Mode=TwoWay}" , CheckBoxState属性变为真调用 RaisePropertyChanged()相应地 PropertyChanged() .由于此函数的参数是 CheckBoxState , 每个与路径的绑定(bind) CheckBoxState被通知更新自身。

  • 这个调用如何激活<TextBlock Text="{Binding Path=CheckBoxState, Mode=TwoWay}" /> ?使之成为可能的 C# 背后的魔力是什么?
  • 为什么是if (PropertyChanged != null)必要的?谁将 PropertyChanged 设置为什么值?
  • Mode=TwoWay的含义貌似不仅可以发信号通知,绑定(bind)中的其他同名Binding元素发生变化时也可以更新内容,那么OneWay模式呢?我们可以将绑定(bind)设置为仅源或仅目标吗?

最佳答案

How does this call activates ? What's the C#'s magic behind this to make it possible?

此代码创建一个 Binding 对象,它将 TextBlock 的 Text 属性链接到 ViewModel 属性。它还向 ViewModel 的 PropertyChanged 事件添加了一个事件处理程序,以便在 ViewModel 触发 PropertyChanged 事件(具有正确的属性)时更新文本值。

Why is if (PropertyChanged != null) necessary? Who sets up the PropertyChanged to what value?

如果 PropertyChanged 事件为 null,则触发它会导致 NullReferenceException。

The meaning of Mode=TwoWay looks like that it not only can signal the change, but also updates the content when other Binding element with the same name in binding changes, then what about OneWay mode? Can we set a Binding as source only or target only?

绑定(bind)方式有:

  • TwoWay:当 ViewModel 属性改变时改变绑定(bind)值,反之亦然
  • OneWay:仅在 ViewModel 属性更改时更改绑定(bind)值
  • OneWayToSource:仅在绑定(bind)值更改时更改 ViewModel 属性
  • 一次性:在创建应用程序或数据上下文更改时将绑定(bind)值设置为 ViewModel 属性的值。

您可以在这里阅读更多关于它们的信息:http://msdn.microsoft.com/en-us/library/system.windows.data.bindingmode.aspx

关于c# - WPF INotifyPropertyChanged 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6789236/

相关文章:

c# - WebApi(MVC 5) PUT 方法不允许使用 HTTP/1.1 405 方法

wpf - {RelativeSource PreviousData} 与 DataGrid 一起使用时始终为 NULL

c# - WPF 控件的公共(public)类修饰符

c# - 调用线程必须是 STA,因为在 WPF 中很多 UI 组件都需要这个错误。在 form.show() 上

WPF 和 MVVM - 动态改变主题

c# - 远程服务模拟显示用户 'NT Authority\Anonymous Logon' 登录失败

c# - 编写我们自己的 Dispose 方法而不是使用 Idisposable

c# - Windows Phone 8.1 检查密码是否设置否则加载新页面

.net - 在 WPF 和 Silverlight 中的静态属性上实现 INotifyProperty 更改

c# - 将左列添加到 MVC Net Core 元素模板 View 中,该 View 会随着用户向下滚动而消失