绑定到.NET中多个索引的属性? [英] Binding to multiple indexed properties in .NET?
问题描述
绑定到这个属性是相当简单:
Binding to this property is fairly simple:
public Foo MyFoo {get; set;}
public class Foo
{
public object this[object key]
}
由于这里在XAML中,你可以这样做:
Because here in XAML you can do:
<Label Content="{Binding MyFoo["key"]}"/>
但是,如果我有第二个索引属性?
But what if I had a second indexed property?
我知道这是不能直接在C#中,但在VB.NET它。
I know this is not directly possible in C#, but in VB.NET it is.
Default Public Property Item(key As Object) As Object 'equivalent to this[...]'
Public Property Item2(key As Object) As Object 'a second indexed property!'
这些都是一些我已经试过了绑定的:
These are some of the bindings I've tried:
<Label Content="{Binding MyFoo["key"]}"/>
<Label Content="{Binding MyFoo.Item["key"]}"/>
<Label Content="{Binding MyFoo.Item2["key"]}"/>
第一个结合仍然可以工作,但其他两个不会。
The first binding will still work, but the other two won't.
有没有直接的解决办法,还是我需要一个解决办法?
Is there a direct solution to this, or do I need a workaround?
推荐答案
JOBO 的建议后放置VB .NET类集会,并提到它在 C#的项目,
我发现了,为什么你不能的直接的绑定到多个索引的属性。
After Jobo's suggestion to place the VB.NET class in an assembly, and reference to it in a C# project,
I found out why you can't directly bind to multiple indexed properties.
引擎盖下,是为每个生成的两种方法的额外的索引属性: get_X
和 set_X
,其中 X
是索引属性的名称。
Under the hood, two methods are generated for every extra indexed property: get_X
and set_X
, where X
is the name of the indexed property.
这意味着VB.NET索引的属性,未标记为默认
,实际上不存在。这就是为什么在我的问题的XAML绑定说,找不到属性项目
或项目2
...
This means that the VB.NET indexed properties, that are not marked as Default
, actually don't exist. This is why the XAML bindings in my question said that it could not find properties Item
or Item2
...
也许,有多种变通办法问题。你可以,例如,创建一个小的类,它包含一个索引属性,然后在这个类的多个实例的主窗口
。
Probably, there are multiple workarounds for this problem. You could, for example, create a small class that contains an indexer property, and then have multiple instances of that class in your MainWindow
.
另一种解决方法可以是结合 get_X
的返回值,但我只能想象如何刺激性这样会得到的,因为这样一来,你不会得到的好处 INotifyPropertyChanged的
。
Another workaround could be binding to the return value of get_X
, but I could only imagine how irritating this would get, since this way you won't get the benefits of INotifyPropertyChanged
.
这篇关于绑定到.NET中多个索引的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!