Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF databinding to interface and not actual object - casting possible?

Say I have an interface like this:

public interface ISomeInterface { ... } 

I also have a couple of classes implementing this interface;

public class SomeClass : ISomeInterface { ... } 

Now I have a WPF ListBox listing items of ISomeInterface, using a custom DataTemplate.

The databinding engine will apparently not (that I have been able to figure out) allow me to bind to interface properties - it sees that the object is a SomeClass object, and data only shows up if SomeClass should happen to have the bound property available as a non-interface property.

How can I tell the DataTemplate to act as if every object is an ISomeInterface, and not a SomeClass etc.?

Thanks!

like image 356
Rune Jacobsen Avatar asked Nov 29 '08 20:11

Rune Jacobsen


2 Answers

In order to bind to explicit implemented interface members, all you need to do is to use the parentheses. For example:

implicit:

{Binding Path=MyValue} 

explicit:

{Binding Path=(mynamespacealias:IMyInterface.MyValue)} 
like image 152
dummyboy Avatar answered Sep 19 '22 15:09

dummyboy


This answer from Microsoft forums by Beatriz Costa - MSFT is worth reading (rather old):

The data binding team discussed adding support for interfaces a while ago but ended up not implementing it because we could not come up with a good design for it. The problem was that interfaces don't have a hierarchy like object types do. Consider the scenario where your data source implements both IMyInterface1 and IMyInterface2 and you have DataTemplates for both of those interfaces in the resources: which DataTemplate do you think we should pick up?

When doing implicit data templating for object types, we first try to find a DataTemplate for the exact type, then for its parent, grandparent and so on. There is very well defined order of types for us to apply. When we talked about adding support for interfaces, we considered using reflection to find out all interfaces and adding them to the end of the list of types. The problem we encountered was defining the order of the interfaces when the type implements multiple interfaces.

The other thing we had to keep in mind is that reflection is not that cheap, and this would decrease our perf a little for this scenario.

So what's the solution? You can't do this all in XAML, but you can do it easily with a little bit of code. The ItemTemplateSelector property of ItemsControl can be used to pick which DataTemplate you want to use for each item. In the SelectTemplate method for your template selector, you receive as a parameter the item you will template. Here, you can check for what interface it implements and return the DataTemplate that matches it.

like image 37
Mohammad Dehghan Avatar answered Sep 21 '22 15:09

Mohammad Dehghan