C# – 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.?


Best Solution

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


{Binding Path=MyValue}


{Binding Path=(mynamespacealias:IMyInterface.MyValue)}
Related Question