How to respond to a click on a TreeViewItem in an MVVM compatible fashion? RRS feed

  • Question

  • The environment is C#/WPF/MVVM. I have a client who wants to detect a click on a TreeViewItem. Each node in the TreeView is bound to a NodeModel. Clicking on an unselected node selects the node which sends a selection notification to the bound NodeModel. However, clicking on a selected node does nothing since the node is already selected and that is the problem that needs to be solved. The simplest answer is probably to use an interaction event of some sort but the details escape me. Suggestions?

    Richard Lewis Haggard

    Friday, December 13, 2019 3:09 AM

All replies

  • Hi,

    You need to add Microsoft.Expression.Interactions.dll,use command to trigger SelectedItemChanged event.

                    <i:EventTrigger EventName="SelectedItemChanged">
                             Command="{Binding selectItemCommand}"/>

     private ICommand _selectItemCommand;
            public ICommand selectItemCommand
                    if (this._selectItemCommand == null)
                        this._selectItemCommand = new RelayCommand(
                            () => this.cm2Click(),
                            () => this.Cancm2Click());
                    return this._selectItemCommand;
                set { }
            public bool Cancm2Click()
               return true; 
            public void cm2Click()

    Best Regards,


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 13, 2019 7:24 AM
  • Nice, but it doesn't quite fix my problem.

    Clicking on an unselected node works just fine. However, clicking on an already selected node does nothing. The solution supplied does not address that problem. What is needed is to pass the mouse button up event to the backing data object for the TreeViewItem, hence the need for an interactivity event that is tied to a TVI, not the TreeView.

    To review, a TreeView is defined. Each node in the TreeView is a TreeViewItem that gets its data from a NodeModel. A Style defines various aspects about how the node will act. What is the syntax that slides the interaction trigger into this so that a left mouse button up event is passed to the TreeViewItem's backing data object?

    Since my non-disclosure agreement precludes me from copying the real code, I'll try to demonstrate the situation with a (fairly) realistic simulation of the situation.

        <Style x:Key="tvfStyle" TargetType="TreeViewItem">
            <Setter Property="IsExpanded"  Value="{Binding Expanded, Mode=TwoWay}" />
            <Setter Property="IsSelected"  Value="{Binding Selected, Mode=TwoWay}" />
            <!-- For brevity, left out many display related details -->
            <Setter Property="ItemsSource" Value="{Binding Path=Children.View}" />
            <!-- How to insert interaction? Do not need example of command, etc, just how and where to do the interaction command action. -->
             ItemsSource="{Binding TreeViewItems}"
             ItemContainerStyle="{StaticResource tvfStyle}"/>
    Make sense?

    Richard Lewis Haggard

    Saturday, December 14, 2019 2:20 AM