如何WPF INotifyPropertyChanged的工作? [英] How does WPF INotifyPropertyChanged work?

查看:146
本文介绍了如何WPF INotifyPropertyChanged的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是使用WPF / C#绑定一个典型的INotifyPropertyChanged的实现。

 命名空间notifications.ViewModel
{
    类MainViewModel:INotifyPropertyChanged的
    {
        公共常量字符串NamePropertyName =CheckBoxState;
        私人布尔_checkboxstate = TRUE;        公共BOOL CheckBoxState
        {
            {返回_checkboxstate; }
            组
            {
                如果(_checkboxstate ==值)回报;
                _checkboxstate =价值;
                RaisePropertyChanged(NamePropertyName);
            }
        }
        公共事件PropertyChangedEventHandler的PropertyChanged;
        私人无效RaisePropertyChanged(字符串propertyName的)
        {
            如果(的PropertyChanged!= NULL)
            {
                的PropertyChanged(这一点,新PropertyChangedEventArgs(propertyName的));
            }
        }
    }
}

我也有一个XAML code,有一个结合 CheckBoxState

 <网格和GT;
    < StackPanel中的Horizo​​ntalAlignment =中心VerticalAlignment =中心>
        <复选框CONTENT =点击我=器isChecked{绑定路径= CheckBoxState,模式=双向}/>
        < TextBlock的文本={绑定路径= CheckBoxState,模式=双向}/>
    < / StackPanel的>
< /网格和GT;

这是DataContext的和模型之间链接的MainWindow.xaml.cs。

 公共部分类主窗口:窗口
{
    notifications.ViewModel.MainViewModel模式=新notifications.ViewModel.MainViewModel();    公共主窗口()
    {
        的InitializeComponent();
        this.DataContext =模型;
    }
}

当用户设置的复选框,我想会发生什么情况如下:器isChecked 为真,并与{绑定路径= CheckBoxState,模式=双向} CheckBoxState 属性变为真调用 RaisePropertyChanged()并据此的PropertyChanged()。作为参数传递给这个函数是 CheckBoxState ,每一个与路径绑定 CheckBoxState 通知进行自我更新。


  • 请问这个电话启动< TextBlock的文本={绑定路径= CheckBoxState,模式=双向}/> ?什么是C#的魔法背后,使之可能?

  • 为什么如果(的PropertyChanged!= NULL)有必要吗?谁是PropertyChanged的建立到什么样的价值?

  • 模式=双向的含义看​​起来像它不仅能信号的变化,同时也更新内容时,在绑定的改变其他同名的Binding元素,那么关于单向模式是什么?我们可以设置一个绑定源或仅目标?


解决方案

  

这是如何调用激活?什么的C#的落后魔术
  这使人们有可能?


这code创建绑定该文本块的Text属性链接到视图模型属性对象。它还增加了一个事件处理程序的视图模型的PropertyChanged事件更新的文本值时,视图模型触发PropertyChanged事件(用正确的属性)。


  

为什么如果(的PropertyChanged!= NULL)有必要吗?谁设置了
  要的PropertyChanged什么价值?


如果PropertyChanged事件为空,则触发就会造成一个NullReferenceException。


  

模式=双向的意义看起来,它不仅可以指示
  改变,同时也更新内容时与其他元素结合
  在绑定的变化,那么怎么样单向模式相同的名称?我们可以吗
  设置绑定源或只针对?


的结合模式是:


  • 双向:更改绑定的值时,视图模型属性更改,反之亦然

  • 单向:更改绑定的值时,只有视图模型属性更改

  • OneWayToSource:更改视图模型属性时,只有绑定值的变化

  • 一次性:设置绑定值到视图模型属性的值时,刚创建的应用程序或数据环境的变化

您可以阅读更多关于他们在这里:<一href=\"http://msdn.microsoft.com/en-us/library/system.windows.data.bindingmode.aspx\">http://msdn.microsoft.com/en-us/library/system.windows.data.bindingmode.aspx

This is a typical INotifyPropertyChanged implementation for using Binding in WPF/C#.

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));
            }
        }
    }
}

I also have a XAML code that has a binding to CheckBoxState.

<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>

This is the MainWindow.xaml.cs to link between the DataContext and model.

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

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

When the user sets the check box, I think what would happen is as follows : IsChecked becomes true, and with "{Binding Path=CheckBoxState, Mode=TwoWay}", CheckBoxState property becomes true to call RaisePropertyChanged() and accordingly PropertyChanged(). As the parameter to this function is CheckBoxState, every Binding with Path CheckBoxState is notified to update itself.

  • How does this call activates <TextBlock Text="{Binding Path=CheckBoxState, Mode=TwoWay}" />? What's the C#'s magic behind this to make it possible?
  • Why is if (PropertyChanged != null) necessary? Who sets up the PropertyChanged to what value?
  • 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?

解决方案

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

This code creates a Binding object which links the TextBlock's Text property to the ViewModel property. It also adds an event handler to the ViewModel's PropertyChanged event to update the text value when the ViewModel fires the PropertyChanged event (with the right property).

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

If the PropertyChanged event is null, then firing it will cause a 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?

The binding modes are:

  • TwoWay: Changes the bound value when the ViewModel property changes and vice versa
  • OneWay: Changes the bound value when the ViewModel property changes only
  • OneWayToSource: Changes the ViewModel property when the bound value changes only
  • OneTime: Sets the bound value to the value of the ViewModel property just when the application is created or the data context changes.

You can read more about them here: http://msdn.microsoft.com/en-us/library/system.windows.data.bindingmode.aspx

这篇关于如何WPF INotifyPropertyChanged的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆