简体   繁体   English

如何实现INotifyPropertyChanged

[英]How to implement INotifyPropertyChanged

I need help implementing INotifyPropertyChanged in my own data structure class. 我需要在自己的数据结构类中实现INotifyPropertyChanged的帮助。 This is for a class assignment, but implementing INotifyPropertyChanged is an addition that I am doing above and beyond what the rubric requires. 这是用于类分配的,但是实现INotifyPropertyChanged是我正在做的事情,超出了规则的要求。

I have a class named 'BusinessRules' that uses a SortedDictionary to store objects of 'Employee' type. 我有一个名为“ BusinessRules”的类,该类使用SortedDictionary来存储“员工”类型的对象。 I have a DataGridView showing all of my employees, and I want to use my BusinessRules class object as the DataSource for my DataGridView. 我有一个显示所有员工的DataGridView,并且我想将BusinessRules类对象用作DataGridView的数据源。 The BusinessRules container is required for the assignment. 分配需要BusinessRules容器。 I have tried to implement INotifyPropertyChanged in this class, with no success. 我试图在此类中实现INotifyPropertyChanged,但没有成功。

The DataSource that I have working is a BindingList. 我正在使用的数据源是一个BindingList。 Presently, I am using that BindingList as a 'sidecar' container and setting that as my DataSource. 目前,我将那个BindingList用作“ sidecar”容器,并将其设置为我的DataSource。 Every change that I make to my BusinessRules class object is mirrored to my BindingList class object. 我对BusinessRules类对象所做的每个更改都将镜像到我的BindingList类对象。 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. 我试图在BusinessRules中实现INotifyPropertyChanged,但是当我将实例化的BusinessRules对象设置为DataSource时,DataGridView什么都没有显示。 What I suspect the problem to be is with the NotifyPropertyChanged() method. 我怀疑问题是出在NotifyPropertyChanged()方法上。 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. 我不知道该如何传递,也不知道该如何传递。大多数示例都涉及更改名称,但是当将新对象添加到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. 我对在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 . 这是我的BusinessRules源代码的链接

It will be very helpful if you read tutorials on how to implement MVVM . 如果您阅读有关如何实现MVVM的教程,这将非常有帮助。

You'd wanna have a base class that implements INotifyPropertyChanged interface. 您需要一个实现INotifyPropertyChanged接口的基类。 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 . 这是有关如何使用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. 最好阅读不同的教程,学习曲线有点陡峭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM