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

  • Home
  • SEARCH
  • 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 9059169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:54:26+00:00 2026-06-16T14:54:26+00:00

I have a treeview which is created from ItemsSource of SecondViewModel instances, different from

  • 0

I have a treeview which is created from ItemsSource of SecondViewModel instances, different from my Window DataContext.

I want to send the ViewModel that belongs to the TreeViewItem via a `CommandParameter.

The window data context is: MyViewModel.
The treeviewitems data context is: SecondViewModel

I want to pass the SecondViewModel and not MyViewModel.

Therefore,

CommandParameter ="{Binding}" 

Won’t work (as it will send MyViewModel)

Edit: Some Code:

 <TreeView Name="treeView" ItemContainerStyle="{StaticResource TreeViewItemStyle}"  Grid.Row="1" Grid.Column="1">
        <TreeViewItem Header="{Binding ProjectName}">
            <TreeViewItem commandBehaviors:MouseDoubleClick.Command="{Binding SelectOtherTab}" 
                          commandBehaviors:MouseDoubleClick.CommandParameter="{Binding}" //this returns the data context of the window, I want to return the Item Source
                ContextMenu="{StaticResource AddClassMenu}" ItemTemplate="{DynamicResource ClassDataTemplate}" ItemsSource="{Binding ClassCollection}">

How can I send the SecondViewModel?

EDIT:

I want to enable deleting the current item, but the command never gets called for some reason.

Here’s the code:

<TreeViewItem x:Name="treeViewItem"
                ContextMenu="{StaticResource AddClassMenu}" ItemTemplate="{DynamicResource ClassDataTemplate}" ItemsSource="{Binding ClassCollection}">
                <TreeViewItem.ItemContainerStyle>
                    <Style TargetType="TreeViewItem">
              HERE->>          <Setter Property="ContextMenu" Value="{StaticResource RemoveClassMenu}"/>
                        <Setter Property="commandBehaviors:MouseDoubleClick.Command" 
        Value="{Binding ElementName=treeViewItem, Path=DataContext.SelectOtherTab}" />
                        <Setter Property="commandBehaviors:MouseDoubleClick.CommandParameter" 
        Value="{Binding }" />
                    </Style>
</TreeViewItem>

My Context Menu:

 <ContextMenu x:Key="RemoveClassMenu">
    <MenuItem Header="Delete" Command="{Binding ElementName=treeViewItem, Path=DataContext.RemoveClass}" CommandParameter="{Binding}"/>
</ContextMenu>

As mentions before, the command just never gets called. What is the problem with my code?

  • 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-06-16T14:54:27+00:00Added an answer on June 16, 2026 at 2:54 pm

    I think what you’re looking to do is set your DoubleClick commands on your child TreeViewItems, not your parent TreeViewItem which sets the ItemsSource

    Right now your XAML is saying to build a parent TreeViewItem, and under that build a bunch of child TreeViewItems for each item in ClassCollection. When you double click on the parent TreeViewItem then run the SelectOtherTab command, however there’s nothing to specify which child TreeViewItem got clicked.

    Here’s a simplified view of the XAML you have now.

    <TreeView x:Name="treeView">
        <TreeViewItem OnDoubleClick="SelectOtherTab"> <!-- Parent TreeViewItem -->
            <TreeViewItem /><!-- Child TreeViewItems -->
            <TreeViewItem />
            <TreeViewItem />
            ...
        </TreeViewItem>
    </TreeView>
    

    Instead you want to attach the Command and CommandParameter properties to each child TreeViewItem, like this:

    <TreeViewItem.ItemContainerStyle>
      <Style TargetType="TreeViewItem">
        <Setter Property="commandBehaviors:MouseDoubleClick.Command" 
                Value="{Binding ElementName=treeView, Path=DataContext.SelectOtherTab}" />
        <Setter Property="commandBehaviors:MouseDoubleClick.CommandParameter" 
                Value="{Binding }" />
      </Style>
    </TreeViewItem.ItemContainerStyle>
    

    Which will make your simplified XAML look something like this:

    <TreeView x:Name="treeView">
        <TreeViewItem> <!-- Parent TreeViewItem -->
            <TreeViewItem OnDoubleClick="SelectOtherTab" /><!-- Child TreeViewItems -->
            <TreeViewItem OnDoubleClick="SelectOtherTab"/>
            <TreeViewItem OnDoubleClick="SelectOtherTab" />
            ...
        </TreeViewItem>
    </TreeView>
    

    I’m still a bit confused by why you have a parent TreeViewItem and are building child TreeViewItems using it’s ItemsSource, however if that’s not necessary you can simplify your VisualTree by eliminating the parent TreeViewItem like this:

    <TreeView Name="treeView" 
              ItemsSource="{Binding ClassCollection}"
              ItemContainerStyle="{StaticResource TreeViewItemStyle}"  
              Grid.Row="1" Grid.Column="1">
        <TreeView.Resources>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="Header" Value="ProjectName" />
                <Setter Property="ContextMenu" Value="{StaticResource AddClassMenu}" />
                <Setter Property="ItemTemplate" Value="{DynamicResource ClassDataTemplate}" />
    
                <Setter Property="commandBehaviors:MouseDoubleClick.Command" 
                        Value="{Binding ElementName=treeView, Path=DataContext.SelectOtherTab}" />
                <Setter Property="commandBehaviors:MouseDoubleClick.CommandParameter" 
                        Value="{Binding }" />
            </Style>
        </TreeView.Resources>
    </TreeView>
    

    Which will make your TreeView look like this:

    <TreeView x:Name="treeView">
        <TreeViewItem OnDoubleClick="SelectOtherTab" />
        <TreeViewItem OnDoubleClick="SelectOtherTab"/>
        <TreeViewItem OnDoubleClick="SelectOtherTab" />
        ...
    </TreeView>
    
    • 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 which displays my ViewModel. I have a button that
I have a treeview (winforms) which have different item types on it. I have
I have a TreeView that launches a new window when each of its TreeViewItems
I have a GUI that uses a treeview which renders data objects into rows,
I have my own custom control derived from System.Windows.Forms.TreeView which is present on the
I have created a TreeView which is actually what it could be called a
I have a treeview and a imageList which contain 1 icon (folder.ico), I want
Question: I have a view which I want to derive from a recursive query.
I have a treeview that represents different filter items to a set of records.
I have a treeview which contains, per node, a key and text. However, there

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.