Question

I have a relatively straightforward hierarchical data master-detail WPF window, developed using MVVM, where the Window points to a ViewModel which exposes as one of its properties the ViewModel for the detail view:

Public Class MasterViewModel
Inherits ViewModelBase ' Contains an implementation of CommandSink
Private WithEvents _ViewerSelection As DetailViewModel
Public Property ViewerSelection As DetailViewModel
    Set(value As DetailViewModel)
        _ViewerSelection = value
        RaisePropertyChanged("ViewerSelection")
    End Set
    Get
        Return _ViewerSelection
    End Get
End Property

ViewModelBase contains an implementation of Josh Smith's VMCommanding code, which I ported to Visual Basic using the Roslyn stuff.

The XAML for the Window looks basically like this, with irrelevant details and a lot of closing brackets ommitted.

Selecting an item in the "Master" ListBox sets the DataContext of the "Detail" ListBox to a corresponding DetailViewModel. Selecting a second item in that list box changes the DataContext of the "Detail" box to the corresponding DetailViewModel of the selection. For performance reasons, once selected, the DetailViewModel is retained and switched in and out of the DataContext of the "Detail" box.

Selecting an item in the "Detail" ListBox triggers a RoutedCommand which is supposed to be caught by the ViewModel selected in the "Master" box. However, the RoutedCommands never change their focus to the correct DetailViewModel; the command is always caught by the DetailViewModel corresponding to the first "Master" ListBox selection.

What do I need to do differently?

<dx:DXWindow 
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
xmlns:local="clr-namespace:MyProject" 
Title="{Binding ProjectTitle}" 
local:CommandSinkBinding.CommandSink="{Binding}">
<dx:DXWindow.Resources>
         <local:MasterViewModel x:Key="MViewM"/>
</dx:DXWindow.Resources>
<dx:DXWindow.DataContext>
    <Binding Source="{StaticResource MViewM}" />
</dx:DXWindow.DataContext>
<Grid>
        <ListBox ItemsSource="{Binding MasterListBox}">
        <-- Selecting an item in the top list box causes a change to the DataContext
        <ListBox ItemTemplate="{StaticResource MaterialDataTemplate}" 
                Name="DetailListBox" 
                DataContext="{Binding ViewerSelection}" 
                ItemsSource="{Binding Materials}" 
                local:CommandSinkBinding.CommandSink="{Binding}" 
                SelectionMode="Multiple">
            <ListBox.CommandBindings>
                <local:CommandSinkBinding  Command="local:DetailVM.Filter"/>
            </ListBox.CommandBindings>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="local:DetailVM.Filter" CommandParameter="{Binding ElementName=DetailListBox, Path=SelectedItems}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </ListBox>
Was it helpful?

Solution

I solved this by using DelegateCommands from the Prism library.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top