Binding to several indexed properties in .NET?

Linking to this property is quite simple:

public Foo MyFoo {get; set;} public class Foo { public object this[object key] } 

Because here in XAML you can do:

 <Label Content="{Binding MyFoo["key"]}"/> 

But what if I had a second indexed property?

I know that this is not possible directly in C #, but it is in VB.NET.

 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 tried:

 <Label Content="{Binding MyFoo["key"]}"/> <Label Content="{Binding MyFoo.Item["key"]}"/> <Label Content="{Binding MyFoo.Item2["key"]}"/> [" key "]}" /> <Label Content="{Binding MyFoo["key"]}"/> <Label Content="{Binding MyFoo.Item["key"]}"/> <Label Content="{Binding MyFoo.Item2["key"]}"/> [" key "]}" /> <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 will not.

Is there a direct solution for this or do I need a workaround?

+1
source share
1 answer

After Jobo proposal to deploy VB.NET class in an assembly and reference it in the project, C #, <br> I found out why you can not directly bind to several indexed properties.

Under the hood, for each additional indexed properties are two methods: get_X and set_X , where X - is the name of the indexed properties.

This means that the indexed properties VB.NET, which are not marked as Default , does not actually exist. That is why binding XAML in my question said that he can not find the properties Item or Item2 ...

Probably, for this problem, there are several workarounds. For example, you can create a small class that contains an indexer property and then have multiple instances of this class in MainWindow .

Another workaround may be binding to the return value get_X , but I could only imagine how it would be annoying, since you will not get the benefits of INotifyPropertyChanged .

+2
source

All Articles