Why use INotifyPropertyChanged with bindings in WPF?

I noticed that almost every example that I find on the Internet about bindings has a class (which links to another property) that inherits the INotifyPropertyChanged interface and uses the method in terms of the set of class properties.

I tried to remove this part from the binding example, and it worked the same way as with the method.

Here is an example. I changed it so that it was TwoWay binding mode and displayed the changed property in the message box.

I did this to play around with the bindings a bit, but now I really don't know why this interface is used

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> 

Main.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"; } } } 

MyData Class:

 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)); } } } } 
+8
source share
2 answers

You do not need INotifyPropertyChanged if you only intend to use the binding to write to the property (as you found out), but you need it so that you can say that someone else wrote the property and update the displayed value accordingly.

To find out what I'm talking about, add a button to your window that, when clicked, directly changes the value of the associated property (and not the corresponding attribute of the interface element associated with this property). With INotifyPropertyChanged you will see that the user interface is updated to a new value when a button is clicked; without it, the user interface will still show the "old" value.

+21
source

From the discussions here, I think you missed the implementation

 RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Propety Name")) 

Once this is implemented, you will see that the user interface is updated automatically. You can view the details on MSDN or the short version on my blog here .

0
source

All Articles