C# 如何实现INotifyPropertyChanged

C# 如何实现INotifyPropertyChanged,c#,datagridview,inotifypropertychanged,C#,Datagridview,Inotifypropertychanged,我需要帮助在我自己的数据结构类中实现INotifyPropertyChanged。这是一个类赋值,但实现INotifyPropertyChanged是我所做的一个补充,超出了规范的要求 我有一个名为“BusinessRules”的类,它使用SortedDictionary来存储“Employee”类型的对象。我有一个显示所有员工的DataGridView,我想使用我的BusinessRules类对象作为DataGridView的数据源。分配需要BusinessRules容器。我尝试在这个类中实现

我需要帮助在我自己的数据结构类中实现INotifyPropertyChanged。这是一个类赋值,但实现INotifyPropertyChanged是我所做的一个补充,超出了规范的要求

我有一个名为“BusinessRules”的类,它使用SortedDictionary来存储“Employee”类型的对象。我有一个显示所有员工的DataGridView,我想使用我的BusinessRules类对象作为DataGridView的数据源。分配需要BusinessRules容器。我尝试在这个类中实现INotifyPropertyChanged,但没有成功

我使用的数据源是一个绑定列表。目前,我将BindingList用作“sidecar”容器,并将其设置为数据源。我对BusinessRules类对象所做的每个更改都会镜像到BindingList类对象。但这显然是草率的编程,我想做得更好

我曾尝试在BusinessRules中实现INotifyPropertyChanged,但当我将实例化的BusinessRules对象设置为数据源时,DataGridView没有显示任何内容。我怀疑问题出在NotifyPropertyChanged()方法上。我不知道该传递什么信息,也不知道该如何处理传递的信息。大多数示例都涉及更改名称,但我更关心的是在SortedDictionary中添加新对象时

    private void NotifyPropertyChanged( Employee emp )
    {
        PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( emp.FirstName ) );
    }
我需要改变什么才能让它工作?你能解释一下为什么我的尝试无效吗

众所周知,我不善于在StackOverflow上提出问题。这不是故意的。请告诉我您还需要什么信息,我会尽快提供


.

如果您阅读上的教程,将非常有帮助

您希望有一个实现
INotifyPropertyChanged
接口的基类。因此,所有视图模型都应该继承自该基类

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
}
您的视图模型应该如下所示

public sealed class BusinessRules : ViewModelBase
下面是一个关于如何利用
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
    }
}

我真的不知道你想做什么,所以我就这样留下我的例子。最好阅读不同的教程,学习曲线有点陡峭。

如果您在上阅读教程,将非常有帮助

您希望有一个实现
INotifyPropertyChanged
接口的基类。因此,所有视图模型都应该继承自该基类

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
}
您的视图模型应该如下所示

public sealed class BusinessRules : ViewModelBase
下面是一个关于如何利用
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
    }
}
我真的不知道你想做什么,所以我就这样留下我的例子。最好阅读不同的教程,学习曲线有点陡峭