views:

75

answers:

3

Can WPF bind to a generic class I created?

It is basically Class<T> where T:MyInterface
Can I Bind to Class<T> and use MyInterface to Bind It?

+1  A: 

Surely, why wouldn't it? Class<T> is just a type. Should bind just fine.

Brian Rudolph
+1  A: 

WPF can bind to CLR objects. The properties being exposed should support notification (ala, INotifyPropertyChanged) or should be of a type which take care of that (ala, ObservableCollection).

With those in place, the object should serve well for supplying data.

Eric
+2  A: 

WPF can bind to any CLR object (or COM object exposed via the CLR), including instances of generic classes. The object can optionally implement INotifyPropertyChanged or subclass DependencyObject so that changes to its properties will be detected by the binding. If not, the property value is copied once when the binding is evaluated, but not updated.

Technically WPF does not "bind to a class": It binds to an object which is an instance of that class. So taken literally the answer to your question would be "no," but I assume you were asking if it could bind to an instance of a generic class in which case the answer is "yes."

WPF does have a limitation as regarding generic classes: The XAML parser cannot create instances of generic classes or reference them within an {x:Type } markup extension, so for most uses with XAML you must create a concrete subclass of your generic class and reference that in your XAML. The exception to this is that the outermost element in a XAML can use the x:TypeArguments attribute to specify a generic class.

Ray Burns