I'm trying to use a collection that is a property of another collection to bind to a listbox. 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, does not work. Does anyone know how I can associate the index in my xaml with the property in my viewmodel?
A binding within a binding is not possible, So in XAML you can't bind to "index".
a. Chris Moser's method, You can create a DependencyProperty that binds to "index" Specify a change listener on the RegisterAttached handler and do your work there.
b. Use a Converter. You can provide index as the ConverterParameter
c. Bind to a POCO property. A POCO property would need its INotifyPropertyChanged signaled by the changer
where index is an integer in my viewmodel, does not work. Does anyone know how I can associate the index in my xaml with the property in my viewmodel?
One simple option would be to just expose a CurrentLocation
property within your ViewModel, which was effectively Location[index]
. You could then bind to it directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With