Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
568 views
in Technique[技术] by (71.8m points)

c# - How does WPF INotifyPropertyChanged work?

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

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?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...