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!
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.
The short answer is DataTemplate's do not support interfaces (think about multiple inheritance, explicit v. implicit, etc). The way we tend to get around this is to have a base class things extend to allow the DataTemplate specialization/generalization. This means a decent, but not necessarily optimal, solution would be:
public abstract class SomeClassBase
{
}
public class SomeClass : SomeClassBase
{
}
<DataTemplate DataType="{x:Type local:SomeClassBase}">
<!-- ... -->
</DataTemplate>
You have another option. Set a Key on your DataTemplate and reference that key in the ItemTemplate. Like this:
<DataTemplate DataType="{x:Type documents:ISpecificOutcome}"
x:Key="SpecificOutcomesTemplate">
<Label Content="{Binding Name}"
ToolTip="{Binding Description}" />
</DataTemplate>
then reference the template by key where you want to use it, like this:
<ListBox ItemsSource="{Binding Path=SpecificOutcomes}"
ItemTemplate="{StaticResource SpecificOutcomesTemplate}"
>
</ListBox>