2016-10-12 4 views
0

私の独自のデータ構造クラスでINotifyPropertyChangedを実装する際に助けが必要です。これはクラスの割り当てのためのものですが、INotifyPropertyChangedを実装することは、私が上で行っていることです。INotifyPropertyChangedを実装する方法

私は 'Employee'型のオブジェクトを格納するSortedDictionaryを使用する 'BusinessRules'という名前のクラスを持っています。私はすべての従業員を示すDataGridViewを持っており、DataGridViewのDataSourceとしてBusinessRulesクラスオブジェクトを使用したいと考えています。割当にはBusinessRulesコンテナが必要です。私はこのクラスでINotifyPropertyChangedを実装しようとしましたが、成功しませんでした。

私が働いているDataSourceはBindingListです。現在、私はそのBindingListを「サイドカー」コンテナとして使用しており、これをデータソースとして設定しています。私のBusinessRulesクラスオブジェクトに加えたすべての変更は、BindingListクラスオブジェクトに反映されます。しかし、これは明らかにうんざりしたプログラミングであり、私はより良くしたいです。

BusinessRulesでINotifyPropertyChangedを実装しようとしましたが、インスタンス化されたBusinessRulesオブジェクトをDataSourceとして設定すると、DataGridViewに何も表示されません。問題はNotifyPropertyChanged()メソッドにあると思われます。私はこれに何を渡すべきか、何が渡されるのかを知らない。ほとんどの例は名前を変更することを扱うが、新しいオブジェクトがSortedDictionaryに追加されると、より心配している。

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

これを実行するには何を変更する必要がありますか?私の試みがうまくいかない理由を説明しますか?

私はStackOverflowで自分の質問を形成することについて悪名高く悪いです。これは意図的なものではありません。あなたが必要とするその他の情報を教えてください。できるだけ早く提供します。

Here is a link to my BusinessRules source code

答えて

2

how to implement MVVMでチュートリアルを読むと非常に役に立ちます。

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 
    } 
} 

私は本当にあなたがそう私はこのような私の例を残しておきます何をしたいのか分かりません。別のチュートリアルを読むと、学習曲線は少し急峻です。

関連する問題