Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change notification without INotifyPropertyChanged? (excerpt from Pro WPF in C# 2010)

While reading Pro WPF in C# 2010 the author writes:

"You can raise an event for each property. In this case, the event must have the name PropertyNameChanged (for example, UnitCostChanged). It’s up to you to fire the event when the property is changed."

Could someone confirm this feature works? I was experimenting and not able to reproduce this behavior (I want to see if this works so then I can do some experimenting with System.Reflection.Emit to create dynamic types)

EDIT: I should clarify the emphasis here is to implement change notification WITHOUT implementing INotifyPropertyChanged, as this is what the book is claiming

Here's the POCO I am testing with:

public class Employee
{
    private string _FirstName;
    public string FirstName 
    {
        get
        {
            return _FirstName;
        }
        set
        {
            if (_FirstName != value)
            {
                _FirstName = value;
                if (FirstNameChanged != null) 
                {
                    FirstNameChanged(this, new PropertyChangedEventArgs("FirstName"));
                }
            }
        }
    }
}

I bound it to a DataGrid and have a timer in the background update the FirstName property randomly every few seconds but the DataGrid never fires

<DataGrid x:Name="dgEmployees" ItemsSource="{Binding ElementName=mainWindow, Path=MyEmployees}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="FirstName" Binding="{Binding Path=FirstName}" />
        </DataGrid.Columns>
    </DataGrid>

The FirstNameChanged event is always null (I thought the binding engine might automatically subscribe to it if it detected it according to the naming convention). MyEmployees is just an ObservableCollection

Can someone confirm if this feature the author mentions, does indeed work and if I'm making a mistake?

EDIT: for the benefit of anyone who thinks I'm misinterpreting the text:

"You can use three approaches to solve this problem:

You can make each property in the Product class a dependency property using the syntax you learned about in Chapter 4. (In this case, your class must derive from DependencyObject.) Although this approach gets WPF to do the work for you (which is nice), it makes the most sense in elements—classes that have a visual appearance in a window. It’s not the most natural approach for data classes like Product.

You can raise an event for each property. In this case, the event must have the name PropertyNameChanged (for example, UnitCostChanged). It’s up to you to fire the event when the property is changed.

You can implement the System.ComponentModel.INotifyPropertyChanged interface, which requires a single event named PropertyChanged. You must then raise the PropertyChanged event whenever a property changes and indicate which property has changed by supplying the property name as a string. It’s still up to you to raise this event when a property changes, but you don’t need to define a separate event for each property."

like image 946
blue18hutthutt Avatar asked Dec 01 '12 14:12

blue18hutthutt


2 Answers

I would say it's very possible to implement change notifications in your POCO's without using either INotifyPropertyChanged or dependency properties, like it's being claimed in the book, unless i'm completely missing the point of the question.

If you raise an event called {Propertyname}Changed from your POCO when the value of a property has changed, the WPF binding system will pick that up, and update the relevant bindings.

See this small program for a demonstration - it's the simplest thing I could think of, but I guess it should work in your case as well.

XAML:

<StackPanel>
    <TextBlock Text="{Binding Name}" />
    <Button Content="Change name" Click="changeNameClick" />
</StackPanel>

Code-behind:

public partial class Window1 : Window
{
    private SimpleObject so = new SimpleObject();

    public Window1()
    {
        InitializeComponent();

        this.so = new SimpleObject();
        so.Name = "Initial value";

        this.DataContext = so;

        var t = new DispatcherTimer(
            TimeSpan.FromSeconds(1), 
            DispatcherPriority.Normal,
            (s, e) => { this.so.Name = "Name changed at: " + DateTime.Now.ToString(); },
            Dispatcher);
    }

    private void changeNameClick(object sender, RoutedEventArgs e)
    {
        this.so.Name = "New value!!";
    }
}

public class SimpleObject
{
    private string mName = null;

    public string Name 
    {
        get { return mName; }
        set
        {
            if (mName != value)
            {
                mName = value;

                if (NameChanged != null)
                    NameChanged(this, EventArgs.Empty);
            }
        }
    }

    public event EventHandler NameChanged;
}
like image 142
Peter Hansen Avatar answered Oct 22 '22 03:10

Peter Hansen


The pattern you mention applies to WPF as well as to Windows Forms (WinForms).

  • WPF, see: "Providing Change Notifications" section in Binding Sources Overview on MSDN.

  • WinForms, see: How to: Apply the PropertyNameChanged Pattern on MSDN.

like image 3
Olivier Jacot-Descombes Avatar answered Oct 22 '22 05:10

Olivier Jacot-Descombes