如何才能WPF绑定的索引属性和列表元素区分? [英] How can a WPF binding distinguish between an indexer property and a list element?

查看:659
本文介绍了如何才能WPF绑定的索引属性和列表元素区分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种形式的结合:

Path=SpecialCollection[0]



SpecialCollection类继承的ObservableCollection并有一个索引属性。

The SpecialCollection class extends ObservableCollection and has an indexer property.

public T this[string propertyValue]
{
    get
    {
        // do stuff
        return default(T);
    }
}



我的问题是,结合的尝试,以获得索引属性值,而不是收集在返回的第0项。有没有办法来强制约束力对待0作为一个整数,因此返回而不是调用集合的索引器属性的getter集合元素?

My problem is that the binding attempts to get the indexer property value, instead of returning the 0th item in the collection. Is there a way to force the binding to treat 0 as an integer so it returns a collection element, instead of invoking the collection's indexer property's getter?

推荐答案

根据MSDN 你可以告诉绑定值的类型输入为指数:

According to MSDN you can tell the binding the type of the value entered as index:

在索引你可以通过逗号分隔的多个索引参数(,)。每个参数的类型可以用括号内指定。例如,你可以有路径=[(SYS:的Int32)42,(SYS:Int32)已24],在那里SYS被映射到系统命名空间

Inside indexers you can have multiple indexer parameters separated by commas (,). The type of each parameter can be specified with parentheses. For example, you can have Path="[(sys:Int32)42,(sys:Int32)24]", where sys is mapped to the System namespace.

我注意到 绑定构造走路径字符串使用另一个 的PropertyPath 构造比默认的的PropertyPath 输入变换器,所述的PropertyPath 构造方法不执行的在这种情况下工作。为了避免这个问题避开绑定构造通过设置路径属性手动它调用通过类型转换器的转换。

I noticed that the Binding constructor taking a path string uses another PropertyPath constructor than the default PropertyPath type converter, said PropertyPath constructor does not work in this scenario. To avoid the problem avoid the Binding constructor by setting the Path property manually which invokes the conversion via type converter.

<!-- Does not work -->
<TextBlock Text="{Binding [(sys:Int32)0]}"/>
<!-- Does work -->
<TextBlock Text="{Binding Path=[(sys:Int32)0]}"/>

这篇关于如何才能WPF绑定的索引属性和列表元素区分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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