Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 853977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:50:51+00:00 2026-05-15T07:50:51+00:00

I have a TreeView WPF with 2 Levels of Data. I have deleted the

  • 0

I have a TreeView WPF with 2 Levels of Data.
I have deleted the ToogleButton from the TreeViewItemTemplate. Now I’d like to expand / collapse the groups with a single mouse-click (not with a double click as the default behaviour).
I have tried in this way:

Private Sub tvArt_SelectedItemChanged(ByVal sender As System.Object, _
    ByVal e As RoutedPropertyChangedEventArgs(Of System.Object)) Handles tvArt.SelectedItemChanged

    If e.NewValue Is Nothing = False Then
        Dim ri As P_RicambiItem = TryCast(e.NewValue, P_RicambiItem)
        If ri Is Nothing = False Then
            If ri.isExpanded Then
                ri.isExpanded = False
            Else
                ri.isExpanded = True
            End If
            ri.isSelected = False
        End If
    End If
End Sub

Using my properties isExpanded and isSelected in the collection data source.

But it works only halfway: after the first click, infact, I can’t click for a second time on the same item, because, even if I’ve deselected it, the event handler “remembers” that it was the last selected item and it doesn’t capture the event SelectedItemChanged.

How can I do?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:50:51+00:00Added an answer on May 15, 2026 at 7:50 am

    Easiest way is probably to handle the PreviewMouseDown event on the TreeView and identify when the mouse clicks the parent TreeViewItems:

    XAML:

    <TreeView Name="treeView1" TreeView.PreviewMouseDown="OnTreeViewPreviewMouseDown" />
    

    Code-behind:

    private void OnTreeViewPreviewMouseDown( object sender, MouseButtonEventArgs e )
    {
        TreeViewItem tvi = GetTreeViewItemClicked( (FrameworkElement) e.OriginalSource, treeView1 );
        if ( null == tvi ) return;
        tvi.IsExpanded = !tvi.IsExpanded;
    }
    
    private static TreeViewItem GetTreeViewItemClicked( UIElement sender, UIElement treeView )
    {
        Point p = sender.TranslatePoint( new Point( 0, 0 ), treeView );
        var obj = treeView.InputHitTest( p ) as DependencyObject;
        while ( obj != null && !( obj is TreeViewItem ) )
        {
            obj = VisualTreeHelper.GetParent( obj );
        }
        return obj as TreeViewItem;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF TreeView with 2 levels of data and 2 HierarchicalDataTemplate to
I have a WPF treeview with dynamic levels (TreeView Item uses a Hierachical data
Say I have a 3-level data-bound WPF TreeView like this one: a aa aaa
In WPF, I have a custom control that inherits from TreeView. The code is
I have a custom class that I would like to bind a WPF TreeView
I have a control that inherits from TreeView (System.Windows.Controls.TreeView from WPF Framework) and it
I have a wpf c# application, that loads tasks to a treeView from a
Hey guys, I have a WPF TreeView that has three nodes, I would like
When I have a control like a WPF Treeview, Naturally when the contents of
I have a TreeView implemented in WPF that is bound to some XML data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.