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 3790162
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:16:19+00:00 2026-05-19T12:16:19+00:00

The WPF Treeview responds to + and – keystrokes to expand and collapse nodes

  • 0

The WPF Treeview responds to + and - keystrokes to expand and collapse nodes in the tree. Great!

Is there an existing command I can bind my toolbar buttons or menu items to to perform the same actions in the treeview? I don’t see anything related to expand/collapse in the stock command constants.

  • 1 1 Answer
  • 0 Views
  • 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-19T12:16:19+00:00Added an answer on May 19, 2026 at 12:16 pm

    The TreeView handles the expansion of a TreeViewItem with the mouse by binding ToggleButton.IsChecked to TreeViewItem.IsExpanded in the ControlTemplate and handles expansion with the keyboard in an override TreeViewItem.OnKeyDown. So, no, it doesn’t use commands in its implementation.

    But you can add your own commands without much effort. In this example, I’ve added a behavior to a TreeView so that it responds to the standard Open and Close application commands:

    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="Open" CommandTarget="{Binding ElementName=treeView1}" Command="Open"/>
            <MenuItem Header="Close" CommandTarget="{Binding ElementName=treeView1}" Command="Close"/>
        </Menu>
        <TreeView>
            <i:Interaction.Behaviors>
                <local:TreeViewCommandsBehavior/>
            </i:Interaction.Behaviors>
            <TreeViewItem Header="Root">
                <TreeViewItem Header="Item1">
                    <TreeViewItem Header="Subitem1"/>
                    <TreeViewItem Header="Subitem2"/>
                </TreeViewItem>
                <TreeViewItem Header="Item2">
                    <TreeViewItem Header="Subitem3"/>
                    <TreeViewItem Header="Subitem4"/>
                </TreeViewItem>
            </TreeViewItem>
        </TreeView>
    </DockPanel>
    

    and here is the behavior that makes that work:

    public class TreeViewCommandsBehavior : Behavior<TreeView>
    {
        private TreeViewItem selectedTreeViewItem;
    
        protected override void OnAttached()
        {
            AssociatedObject.AddHandler(TreeViewItem.SelectedEvent, new RoutedEventHandler(TreeViewItem_Selected));
            AssociatedObject.CommandBindings.Add(new CommandBinding(ApplicationCommands.Open, CommandExecuted));
            AssociatedObject.CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, CommandExecuted));
        }
    
        private void TreeViewItem_Selected(object sender, RoutedEventArgs e)
        {
            selectedTreeViewItem = e.OriginalSource as TreeViewItem;
        }
    
        private void CommandExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            bool expand = e.Command == ApplicationCommands.Open;
            if (selectedTreeViewItem != null)
                selectedTreeViewItem.IsExpanded = expand;
        }
    }
    

    If you are not familiar with behaviors, first add this namespace:

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    

    and add the corresponding reference to your project.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I expand all TreeView nodes in WPF? In WinForms there was a
Is there a way to show Root lines in a WPF Treeview? Or do
How do I make the TreeViewItem(s) of an existing WPF TreeView raise a Click
I don't know how to bind a List of Drink to a WPF TreeView.
I have a custom class that I would like to bind a WPF TreeView
I need to add images to WPF treeview nodes, I've had a look at
I am trying to bind an IList to a WPF TreeView in a hierarchal
How can I retrieve the item that is selected in a WPF-treeview? I want
Hey guys, I have a WPF TreeView that has three nodes, I would like
I have some questions about treeview in WPF. How can I change header of

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.