索引器属性的 PropertyChanged
我有一个带有索引器属性的类,带有字符串键:
public class IndexerProvider {
public object this[string key] {
get
{
return ...
}
set
{
...
}
}
...
}
我使用索引器符号绑定到 WPF 中此类的实例:
<TextBox Text="{Binding [IndexerKeyThingy]}">
效果很好,但我想在以下情况下引发一个 PropertyChanged
事件:索引器值发生变化。 我尝试使用属性名称“[keyname]”(即在键名称周围包含 [])来引发它,但这似乎不起作用。 我的输出窗口中没有出现任何绑定错误。
我无法使用 CollectionChangedEvent,因为索引不是基于整数的。 从技术上讲,该对象无论如何都不是一个集合。
我可以这样做吗?怎样做?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据 此博客条目,您必须使用
“Item[]”
。 Item 是编译器在使用索引器时生成的属性的名称。如果您想明确,可以使用 IndexerName 属性。
这将使代码看起来像:
至少它使意图更加清晰。 不过,我不建议您更改索引器名称,如果您的朋友发现字符串
"Item[]"
是硬编码的,则可能意味着 WPF 将无法处理不同的索引器名称。According to this blog entry, you have to use
"Item[]"
. Item being the name of the property generated by the compiler when using an indexer.If you want to be explicit, you can decorate the indexer property with an IndexerName attribute.
That would make the code look like:
At least it makes the intent more clear. I don't suggest you change the indexer name though, if your buddy found the string
"Item[]"
hard coded, it probably means that WPF would not be able to deal with a different indexer name.另外,您可以使用
“仅通知绑定到索引器上 IndexerKeyThingy 的控件”。
Additionaly, you can use
To notify only controls bound to IndexerKeyThingy on your indexer.
在处理 INotifyPropertyChang(ed/ing) 和索引器时,至少还有一些额外的注意事项。
首先是大多数避免魔法的流行方法属性名称字符串无效。 由
[CallerMemberName]
属性末尾缺少“[]”,并且 lambda 成员表达式根本无法表达该概念。几个其他帖子使用了
Binding.IndexerName
以避免字符串文字"Item[]",这是合理的,但引发了第二个潜在问题。 对 WPF 相关部分的反汇编的调查在 PropertyPath.ResolvePathParts 中发现了以下片段。
重复使用
"Item[]"
作为常量值表明 WPF 期望它是在 PropertyChanged 事件中传递的名称,并且,即使它不关心实际属性是什么调用(我没有确定以某种方式令我满意),避免使用[IndexerName]
将保持一致性。There are at least a couple of additional caveats when dealing with INotifyPropertyChang(ed/ing) and indexers.
The first is that most of the popular methods of avoiding magic property name strings are ineffective. The string created by the
[CallerMemberName]
attribute is missing the '[]' at the end, and lambda member expressions have problems expressing the concept at all.Several other posts have used
Binding.IndexerName
to avoid the string literal"Item[]"
, which is reasonable, but raises the second potential issue. An investigation of the dissasembly of related parts of WPF turned up the following segment in PropertyPath.ResolvePathParts.The repeated use of
"Item[]"
as a constant value suggests that WPF is expecting that to be the name passed in the PropertyChanged event, and, even if it doesn't care what the actual property is called (which I didn't determine to my satisfaction one way or the other), avoiding use of[IndexerName]
would maintain consistency.实际上,我认为将 IndexerName 属性设置为“Item”是多余的。 IndexerName 属性专门用于重命名索引(如果您想为其集合项指定不同的名称)。 因此,您的代码可能如下所示:
将索引器名称设置为您想要的任何名称后,您就可以在 FirePropertyChanged 事件中使用它。
Actually, I believe setting the IndexerName attribute to "Item" is redundant. The IndexerName attribute is specifically designed to rename an index, if you want to give it's collection item a different name. So your code could look something like this:
Once you set the indexer name to whatever you want, you can then use it in the FirePropertyChanged event.