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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:22:25+00:00 2026-05-29T10:22:25+00:00

I have a usercontrol with a Tree inside it with, the command property binds

  • 0

I have a usercontrol with a Tree inside it with, the command property binds to the DataType and not the DataContext.

How can I redirect the binding to go to the DataContext and Not the DataType ? Also Out of curiosity how would I bind to the UserControl’s DataContext instead of the Tree’s DataContext ?

Here is the code in question:

<HierarchicalDataTemplate 
            DataType="{x:Type viewModel:UsersViewModel}" 
            ItemsSource="{Binding Children}"
            >
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding UserName}">
                    <TextBlock.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="Edit" Command="{Binding EditCommand}" CommandParameter="{Binding UserName}"/>
                                <MenuItem Header="Delete"/>
                            </ContextMenu>
                        </TextBlock.ContextMenu>
                </TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>

It seems to bind to the UsersViewModel and Not the DataContext(AllUsersViewModel).

This is the entire piece of XAML Just in case:

 <Grid Width="150">
<TreeView ItemsSource="{Binding Users}" DataContext="{Binding allUsersViewModel}">
    <TreeView.ItemContainerStyle>
        <!-- 
    This Style binds a TreeViewItem to a TreeViewItemViewModel. 
    -->
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
            <Setter Property="FontWeight" Value="Normal" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="FontWeight" Value="Bold" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </TreeView.ItemContainerStyle>

    <TreeView.Resources>    
        <ContextMenu x:Key="CategoryMenu">
            <MenuItem Header="Add Subcategory" Command="New">
            </MenuItem>

            <MenuItem Header="Remove Category" Command="Delete">
            </MenuItem>
        </ContextMenu>

        <HierarchicalDataTemplate 
            DataType="{x:Type viewModel:UsersViewModel}" 
            ItemsSource="{Binding Children}"
            >
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding UserName}">
                    <TextBlock.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="Edit" Command="{Binding EditCommand}" CommandParameter="{Binding UserName}"/>
                                <MenuItem Header="Delete"/>
                            </ContextMenu>
                        </TextBlock.ContextMenu>
                </TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate 
            DataType="{x:Type viewModel:PermissionCategoryViewModel}" 
            ItemsSource="{Binding Children}"
            >
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding PermissionCategoryName}" />
            </StackPanel>
        </HierarchicalDataTemplate>

        <DataTemplate DataType="{x:Type viewModel:PermissionViewModel}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding PermissionName}" />
            </StackPanel>
        </DataTemplate>
    </TreeView.Resources>
</TreeView>
</Grid> 

Thanks for the Help!

EDIT

Well I tried a few things, but it didnt work out =(. Nothing Happens

I should have mentioned that my MainWindow Has the DataContext and the UserControl Inherits it by placing the UserControl Inside of it.

<Window.DataContext>
    <viewModel:MainViewModel/>
</Window.DataContext>

I tried these different things

Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:AllUsers}}, Path=DataContext.EditCommand}"

and

Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}, Path=DataContext.EditCommand}"

and

Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}, Path=DataContext.EditCommand}"

and

Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:AllUsers}}, Path=DataContext.EditCommand}"

Lastly

Command="{Binding PlacementTarget.DataContext.EditCommand, 
                      RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"
  • 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-29T10:22:26+00:00Added an answer on May 29, 2026 at 10:22 am

    You can use a RelativeSource binding to find the TreeView, then bind to it’s DataContext

    Command="{Binding Path=DataContext.EditCommand,
        RelativeSource={RelativeSource AncestorType={x:Type TreeView}}}"
    

    If you ever want to bind to your UserControl, you can use the same type of binding:

    RelativeSource={RelativeSource AncestorType={x:Type local:MyUserControl}}
    

    Note that RelativeSource bindings will return a reference to the UI object, not it’s DataContext, so if you want to bind to something in the DataContext you have to specify Path=DataContext.SomeValue

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

Sidebar

Related Questions

I have a DataTemplate inside a global/shared ResourceDictionary like this which targets a DataType:
I have an application which shows a Tree where can select nodes of the
I have UserControl 'A' with a label, and this property: /// <summary> /// Gets
I have a usercontrol, that when one property changes, the bindings change for a
I have a UserControl that takes a class object as property DataSource. On DataBind,
I have a usercontrol that I want to have the grid inside so I
I want to create my own expandable/collapsable tree-like UserControl, which nodes are the Border
I've got a working custom markup extension which retrieves information out of the DataContext
I have a WPF UserControl that I'd like to inject dependencies into. What's the
I have three tree view controls which house different (but mostly similar data), as

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.