简体   繁体   English

如何将列表框绑定到位于ViewModel中的复杂类型内的集合?

[英]How to bind a listbox to a collection within a complex type located within my ViewModel?

My view model currently contains a "SelectedClient" property which refers to the selected "Client" object in a datagrid. 我的视图模型当前包含一个“ SelectedClient”属性,该属性引用数据网格中所选的“ Client”对象。 This selected client property contains a property titled "OfficeLocations" which is essentially just a list of "OfficeLocation" objects. 该选定的客户端属性包含一个名为“ OfficeLocations”的属性,该属性实际上只是“ OfficeLocation”对象的列表。

I am trying to bind a listbox to the SelectedClients.OfficeLocations property like so: 我试图像这样将列表框绑定到SelectedClients.OfficeLocations属性:

<ListBox ItemsSource="{Binding SelectedClient.OfficeLocations}" />

But for some reason the ListBox always shows up blank. 但是由于某些原因,ListBox总是空白。 Once again, in debug mode when I view the SelectedClient.OfficeLocations property, it does in fact contain data. 再一次,在调试模式下,当我查看SelectedClient.OfficeLocations属性时,它实际上包含数据。

I've also tried something like: 我也尝试过类似的方法:

<ListBox ItemsSource="{Binding SelectedClient, Path=OfficeLocations}" />

To no avail. 无济于事。

Any ideas would be greatly appreciated..Thanks! 任何想法将不胜感激..谢谢!

Ahh, so it turns out that I was trying to do this binding with a DataGrid.RowDetailsTemplate which overrides my DataContext to SelectedItem already...meaning changing my binding to look like: 嗯,事实证明我正在尝试使用DataGrid.RowDetailsTemplate进行此绑定,它已经将DataContext覆盖到SelectedItem了...意味着将绑定更改为:

 <ListBox ItemsSource="{Binding OfficeLocations, Mode=TwoWay}">

Fixed the issue! 解决了这个问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM