WPF binding index collection

I am trying to use a collection that is a property of another collection to bind to a list. The following works fine

<ListBox ItemsSource="{Binding Path=Locations[0].Buildings}"> 

the problem is that I need a dynamic index and

 <ListBox ItemsSource="{Binding Path=Locations[index].Buildings}"> 

where index is an integer in my viewmodel model does not work. Does anyone know how I can associate an index in my xaml with a property in my view model?

+6
source share
2 answers

where index is an integer in my viewmodel model does not work. Does anyone know how I can associate an index in my xaml with a property in my view model?

One simple option is to simply open the CurrentLocation property in your ViewModel, which was efficiently Location[index] . Then you can bind it directly.

+6
source

Binding in a binding is not possible, so in XAML you cannot bind to an "index".

a. Chris Moser's method . You can create a DependencyProperty that binds to an "index". Specify the change listener in the RegisterAttached handler and do your work there.

b. Use a converter. You can specify an index as the parameter ConverterParameter

with. Bind to POCO property. The POCO property will need its INotifyPropertyChanged, signaled by the changer

+4
source

Source: https://habr.com/ru/post/946437/


All Articles