带索引的WPF绑定集合

带索引的WPF绑定集合

问题描述:

我正在尝试使用一个集合(该集合是另一个集合的属性)来绑定到列表框.以下工作正常

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}">

其中index是我的视图模型中的整数,不起作用.有谁知道如何将xaml中的索引与viewmodel中的属性相关联?

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?

其中index是我的视图模型中的整数,不起作用.有谁知道如何将xaml中的索引与viewmodel中的属性相关联?

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?

一个简单的选择是只在ViewModel中公开一个 CurrentLocation 属性,该属性实际上是 Location [index] .然后,您可以直接绑定到它.

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.