问题
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?
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/16903244/wpf-binding-collection-with-index