gpt4 book ai didi

c# - WPF 绑定(bind)如何区分索引器属性和列表元素?

转载 作者:太空狗 更新时间:2023-10-29 20:57:24 24 4
gpt4 key购买 nike

我有一个表格的绑定(bind):

Path=SpecialCollection[0]

SpecialCollection 类扩展了 ObservableCollection 并具有索引器属性。

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

我的问题是绑定(bind)尝试获取索引器属性值,而不是返回集合中的第 0 个项目。有没有办法强制绑定(bind)将 0 视为整数,以便它返回一个集合元素,而不是调用集合的索引器属性的 getter?

最佳答案

According to MSDN您可以告诉绑定(bind)作为索引输入的值的类型:

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.

我注意到 Binding constructor taking a path string使用另一个 PropertyPath构造函数不是默认的 PropertyPath 类型转换器,表示 PropertyPath 构造函数在这种情况下工作。要避免此问题,请通过手动设置 Path 属性来避免 Binding 构造函数,该属性会通过类型转换器调用转换。

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

关于c# - WPF 绑定(bind)如何区分索引器属性和列表元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5738585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com