WPF数据绑定接口,而不是实际的对象 - 铸造可能吗? [英] WPF databinding to interface and not actual object - casting possible?

查看:182
本文介绍了WPF数据绑定接口,而不是实际的对象 - 铸造可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个这样的接口:

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列表框列出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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆