WPF Binding Collection with Index

百般思念 提交于 2021-02-04 22:12:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!