11

I've notived that practically every example I find on the internet about bindings has a class (which binds to another property) that inherits the INotifyPropertyChanged interface and uses a method in the set part of the class' property.

I've tried removing that part from the binding example and it worked the same as it would with the method.

Here's the example. I've altered it so it would be a TwoWay bindingmode and show the changed property in a messagebox.

I did that just to play around a little bit with bindings, but now I really don't know why that 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));
            }
        }
    }
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
morcillo
  • 1,091
  • 5
  • 19
  • 51

2 Answers2

24

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

To see what I 'm talking about, add a button to your window that when clicked directly changes the value of the bound property (not the corresponding attribute of the UI element bound to that property). With INotifyPropertyChanged, you will see the UI updating itself to the new value when you click the button; without it, the UI will still show the "old" value.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • I see. Thank you very much for that answer. It really helped me. I'll mark it as the correct answer in 5 minutes. I need those 5 minutes because stackoverflow isn't letting me mark the question as answered – morcillo May 06 '12 at 23:33
  • OK I can't mark it as answered because I did that and it didn't work. I uncommented (I don't know if this word is correct .. english isn't my first language) the Inotify part and it should've worked but it didn't. If you want I can send you the thing I did. It's really simple. A textbox that displays the value of the binding, a button that changes it and a button that shows it in a messagebox. The messagebox is working fin, but the textbox isn't updating – morcillo May 06 '12 at 23:39
  • @morcillo: Edit the question and post your XAML and relevant code-behind. I won't be able to answer right now because it's time to hit the sack, but I 'll look at it tomorrow. – Jon May 06 '12 at 23:42
  • @morcillo: Can't see anything wrong at first sight. Without INPC if you click `btnChange` only the textbox should not update; with INPC it should update to "New Binding". – Jon May 07 '12 at 07:47
  • Sorry for taking so long to answer but I've just returned from work. That's the thing. It isn't autoupdating. It just doesn't update de textbox. That's one of the reasons why I couldn't see a point in using INPC. Buth thank you very much. I'll mark your answer as the correct one, because it actually is correct. I'll try to find out what's the problem later. Thank you for your help – morcillo May 07 '12 at 23:23
  • I FOUND THE (excuse me for my english) F**KING PROBLEM. I wasn't letting MyClass inhert INPC. The moment I put " : INotifyPropertyChanged" it worked like a charm. Thank you ver much for your help – morcillo May 07 '12 at 23:48
0

From the discussions here, I think you missed implementing

RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(“Propety Name”))

Once that is implemented you can see that the UI is updating automatically. You can view details on MSDN or a brief version at my blog here.

Scott Baker
  • 10,013
  • 17
  • 56
  • 102
surpavan
  • 1,372
  • 7
  • 34
  • 66