Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Binding Collection with Index

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?

like image 325
aw04 Avatar asked Jun 03 '13 18:06

aw04


2 Answers

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

like image 106
Ady Avatar answered Oct 20 '22 06:10

Ady


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.

like image 39
Reed Copsey Avatar answered Oct 20 '22 06:10

Reed Copsey