带索引的WPF绑定集合 [英] WPF Binding Collection with Index

查看:36
本文介绍了带索引的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.

这篇关于带索引的WPF绑定集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆