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!
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)}
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
andIMyInterface2
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 ofItemsControl
can be used to pick whichDataTemplate
you want to use for each item. In theSelectTemplate
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 theDataTemplate
that matches it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With