有一个DataGrid和一个TabControl的WPF MVVM主详细视图视图、有一个、详细、DataGrid

2023-09-06 19:32:03 作者:跳海自尽的鱼

我想实现在WPF MVVM主详细视图。

I am trying to implement a Master detail view in WPF MVVM.

在我的ViewModel我有原因的观察的集合。每个原因都有解决方案。

In my viewmodel I have a observable collection of "Causes". Each Cause has an observable collection of "Solutions".

我绑定一个可编辑的数据网格的原因,这是工作的罚款。但是,当用户在数据网格选择一行欲允许用户看到的TabControl其相应的解决方法。

I bound an editable Datagrid to the Causes and it is working fine. But when a user selects a row in the DataGrid I want to allow the user to see its associated solutions in the TabControl.

我应该如何去这样做?我应该创建一个属性CurrentCause在视图模型,并将其绑定到的SelectedItem。在TabControl的,我可以绑定到CurrentCause.Solutions。

How should I go about doing this? Should I create a property CurrentCause in the Viewmodel and bind it to the SelectedItem. In the TabControl can I bind to CurrentCause.Solutions.

这会不会是最佳的方法呢?请指教。谢谢!!

Will this be an optimal approach? Please advise. Thanks!!

推荐答案

您可以绑定的ItemsSource 的的TabControl的是的SelectedItem

You could bind the ItemsSource of the TabControl to be the SelectedItem of the DataGrid using element binding.

<TabControl ItemsSource="{Binding ElementName=myDataGrid, Path=SelectedItem.Solutions}">