WPF MVVM Master detail view with a datagrid and a

2019-08-06 10:25发布

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

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

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.

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!!

3条回答
时光不老,我们不散
2楼-- · 2019-08-06 11:00

I would bind your DataGrid to a list of Causes, the SelectedItem to CurrentCause in your Model and the TabControl to Solutions. Then you have everything nicely tied up in your MVVM.

private Cause _currentCause;
public Cause CurrentCause
{
    get { return _currentCause; }
    set
    {
        if (_currentCause == value) return;
            CurrentSolution = _currentCause.Solutions;  //However you do this...
        _currentCause = value;

        RaisePropertyChanged("CurrentCause");
    }
}

private ObservableCollection<Cause> _causes;
public ObservableCollection<Cause> Causes
{
    get { return _causes; }
    set
    {
        _causes = value;
        RaisePropertyChanged("Causes");
    }
}
private ObservableCollection<Solution> _solutions;
public ObservableCollection<Solution> Solutions
{
    get { return _solutions; }
    set
    {
        _solutions = value;
        RaisePropertyChanged("Companies");
    }
}



<dg:DataGrid ItemsSource="{Binding Causes}" SelectedItem="{Binding CurrentCause}"...
查看更多
Juvenile、少年°
3楼-- · 2019-08-06 11:09

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}">
查看更多
forever°为你锁心
4楼-- · 2019-08-06 11:14

You can set IsSynchronizedWithCurrentItem to True and do something like this.

查看更多
登录 后发表回答