Adam Howell
Adam Howell

Reputation: 455

How to implement INotifyPropertyChanged

I need help implementing INotifyPropertyChanged in my own data structure class. This is for a class assignment, but implementing INotifyPropertyChanged is an addition that I am doing above and beyond what the rubric requires.

I have a class named 'BusinessRules' that uses a SortedDictionary to store objects of 'Employee' type. I have a DataGridView showing all of my employees, and I want to use my BusinessRules class object as the DataSource for my DataGridView. The BusinessRules container is required for the assignment. I have tried to implement INotifyPropertyChanged in this class, with no success.

The DataSource that I have working is a BindingList. Presently, I am using that BindingList as a 'sidecar' container and setting that as my DataSource. Every change that I make to my BusinessRules class object is mirrored to my BindingList class object. But this is obviously sloppy programming, and I want to do better.

I have tried to implement INotifyPropertyChanged in BusinessRules, but when I set my instantiated BusinessRules object as the DataSource, the DataGridView shows nothing. What I suspect the problem to be is with the NotifyPropertyChanged() method. I do not know what to pass to this, nor what to do with what is passed in. Most examples deal with changing a name, but I am more concerned when a new object is added to the SortedDictionary.

    private void NotifyPropertyChanged( Employee emp )
    {
        PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( emp.FirstName ) );
    }

What do I need to change in order to get this working? Will you explain why my attempt is not working?

I am notoriously bad about forming my questions on StackOverflow. This is not intentional. Please, let me know what other information you require, and I will provide it as quickly as I can.

Here is a link to my BusinessRules source code.

Upvotes: 0

Views: 2764

Answers (1)

jegtugado
jegtugado

Reputation: 5141

It will be very helpful if you read tutorials on how to implement MVVM.

You'd wanna have a base class that implements INotifyPropertyChanged interface. So all your view models should inherit from this base class.

public class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChangedEvent(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

// This sample class DelegateCommand is used if you wanna bind an event action with your view model
public class DelegateCommand : ICommand
{
    private readonly Action _action;

    public DelegateCommand(Action action)
    {
        _action = action;
    }

    public void Execute(object parameter)
    {
        _action();
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

#pragma warning disable 67
    public event EventHandler CanExecuteChanged;
#pragma warning restore 67
}

Your view model should look like this.

public sealed class BusinessRules : ViewModelBase

Here's an example on how to utilize the RaisePropertyChangedEvent.

public sealed class Foo : ViewModelBase
{
    private Employee employee = new Employee();

    private string Name
    {
        get { return employee.Name; }
        set 
        { 
            employee.Name = value; 
            RaisePropertyChangedEvent("Name"); 
            // This will let the View know that the Name property has updated
        }
    }

    // Add more properties

    // Bind the button Command event to NewName
    public ICommand NewName
    {
        get { return new DelegateCommand(ChangeName)}
    }

    private void ChangeName()
    {
        // do something
        this.Name = "NEW NAME"; 
        // The view will automatically update since the Name setter raises the property changed event
    }
}

I don't really know what you want to do so I'll leave my example like this. Better read different tutorials, the learning curve is a bit steep.

Upvotes: 2

Related Questions