C# – WPF databinding to interface and not actual object – casting possible

cdata-bindingwpf

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!

Best Answer

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)}
Related Topic