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

查看:304
本文介绍了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:


在索引器中,您可以使用逗号分隔多个索引器参数(, )。每个参数的类型可以用括号指定。例如,您可以使用Path =[(sys:Int32)42,(sys:Int32)24],其中sys映射到System命名空间。

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在这种情况下,构造函数不。为避免这个问题,通过手动设置路径属性,通过类型转换器调用转换,避免了绑定构造函数。 p>

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天全站免登陆