WPF 数据绑定到接口而不是实际对象 - 可以转换吗? [英] 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
{
...
}
现在我有一个 WPF ListBox 列出 ISomeInterface 的项目,使用自定义 DataTemplate.
Now I have a WPF ListBox listing items of ISomeInterface, using a custom DataTemplate.
数据绑定引擎显然不会(我已经能够弄清楚)允许我绑定到接口属性 - 它看到该对象是一个 SomeClass 对象,并且只有当 SomeClass 碰巧具有绑定时才会显示数据属性可用作非接口属性.
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.
如何告诉 DataTemplate 就好像每个对象都是 ISomeInterface 而不是 SomeClass 等一样?
How can I tell the DataTemplate to act as if every object is an ISomeInterface, and not a SomeClass etc.?
谢谢!
推荐答案
为了绑定到显式实现的接口成员,您需要做的就是使用括号.例如:
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)}
这篇关于WPF 数据绑定到接口而不是实际对象 - 可以转换吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!