1

I have a datasource with generic type T as below.

public class GridObjectDataSource<T>
{
}

T is coming from external client, where INotifyPropertychanged is not implemented. Can we implement in the above class for any property change in the generic class T? (I need to show the changes in the UI.)

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Please Check [this](http://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist) and [this](http://geekswithblogs.net/brians/archive/2010/07/27/inotifypropertychanged-with-less-code-using-generics-amp-reflection.aspx) might help you!!!! – Abhinav Sharma Mar 12 '14 at 09:52

3 Answers3

2

Yes you can implement that on your generic class as it simply defines an event that must be implemented:

public class MyClass<T> : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
}

The delegate for that property doesn't demand anything more special than the property name and an instance reference to the object raising the event, there is no generics magic required for it.

Your consuming class can simply hook into it:

var instanceOfMyClass = new MyClass<SomeObject>();
instanceOfMyClass.PropertyChanged  += theHandlerForTheEvent;

...or...

instanceOfMyClass.PropertyChanged  += (o, e) => { do something; };

What you appear to be doing actually makes a very good pattern when GridObjectDataSource<T> is used as an abstract base class and derived classes (viewmodels etc.) specify the actual type of T:

public abstract class GridObjectDataSource<T> : INotifyPropertyChanged
{
    public abstract void DoSomething(T someInput);

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

    public event PropertyChangedEventHandler PropertyChanged;
}


public class MySpecificViewModel : GridObjectDataSource<int>
{

    public override void DoSomething(int someInput)
    {

    }
}
slugster
  • 49,403
  • 14
  • 95
  • 145
  • Just be aware that simply implementing the interface does not automatically add the behaviour you'd expect from it. If you change the underlying `T` object directly then your `GridObjectDataSource` has no way of knowing that. And `PropertyChanged` would never be raised. – Dirk Mar 12 '14 at 09:58
  • @Dirk Yes, `T` itself would also have to implement `INotifyPropertyChanged` if you wanted to know when it's properties had changed. – slugster Mar 12 '14 at 10:01
  • If, T has already implemented INotifyPropertyChanged , then the above code is not required.. becoz it updates the object itself only.. – user3355293 Mar 12 '14 at 11:08
  • @user3355293 No, INotifyPropertyChanged is also required on GridObjectDataSource as well. It is required on anything that wants to notify (either directly on, or implemented in a base class). Were you actually meaning something else? – slugster Mar 12 '14 at 11:26
0

The best you can do is define a constraint

public class GridObjectDataSource<T> where T : INotifyPropertyChanged
        {
        }
Georgi-it
  • 3,676
  • 1
  • 20
  • 23
0

As you can make no changes to the generic class T you should have to write viewmodels for your generic classes. If they have a common interface you only need one viewmodel. If they are all different you should have one viewmodel for each type.

See MSDN Patterns - WPF Apps With The Model-View-ViewModel Design Pattern

csteinmueller
  • 2,427
  • 1
  • 21
  • 32