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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:17:36+00:00 2026-05-16T14:17:36+00:00

I have this Tree View that looks like this: <TreeView Grid.Column=0 Grid.Row=2 MinHeight=100 MinWidth=100

  • 0

I have this Tree View that looks like this:

        <TreeView 
                Grid.Column="0"
                Grid.Row="2"
                MinHeight="100" 
                MinWidth="100"
                BorderBrush="White"
                DataContext="{Binding Projects, Source={x:Static SizingApp:Manager.Instance}}">
                <TreeView.Resources>
                    <Style TargetType="TreeViewItem">
                        <Setter Property="IsExpanded" Value="True" />
                    </Style>
                    <DataTemplate x:Key="LoadTemplate">
                        <TextBlock Text="{Binding Name}">
                            <TextBlock.ContextMenu>
                                <ContextMenu>
                                    <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Edit_InputGestureText}">
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name, 
                                                                            StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Edit_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                    <MenuItem>
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name, 
                                                                            StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Save_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                    <MenuItem>
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name, 
                                                                            StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Export_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                    <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Copy_Header}"
                                                                    InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Copy_InputGestureText}"/>
                                    <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Remove_InputGestureText}">
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name, 
                                                                            StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Remove_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                </ContextMenu>
                            </TextBlock.ContextMenu>
                        </TextBlock>
                    </DataTemplate>
                    <HierarchicalDataTemplate x:Key="StepTemplate"
                            ItemsSource="{Binding Loads}" 
                            ItemTemplate="{StaticResource LoadTemplate}">
                        <TextBlock Text="{Binding Name}">
                            <TextBlock.ContextMenu>
                                <ContextMenu>
                                    <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Edit_InputGestureText}">
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name, 
                                                                StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Edit_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                    <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Copy_Header}" 
                                                        InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Copy_InputGestureText}" />
                                    <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Paste_Header}" 
                                                        InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Paste_InputGestureText}" />
                                    <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Import_Load_Header}" />
                                    <MenuItem>
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name, 
                                                                StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Export_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                    <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Remove_InputGestureText}">
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name, 
                                                                StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Remove_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                    <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Add_Load_Header}"
                                                        ItemsSource="{StaticResource mnuLoadSubmenuCommon}"/>
                                </ContextMenu>
                            </TextBlock.ContextMenu>
                        </TextBlock>
                    </HierarchicalDataTemplate>
                    <HierarchicalDataTemplate x:Key="ProjectTemplate"
                            ItemsSource="{Binding Steps}"
                            ItemTemplate="{StaticResource StepTemplate}">
                        <TextBlock Text="{Binding Name}">
                            <TextBlock.ContextMenu>
                                <ContextMenu>
                                    <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Edit_InputGestureText}" 
                                              Click="EditProject_Click">
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name, 
                                                    StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Edit_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                    <MenuItem>
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name,
                                                    StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Save_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                    <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Copy_Header}" 
                                            InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Copy_InputGestureText}" />
                                    <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Paste_Header}" 
                                            InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Paste_InputGestureText}" />
                                    <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Remove_InputGestureText}">
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name,
                                                    StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Remove_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                    <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Project_AddStep_Header}" />
                                    <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Project_ImportStep_Header}" />
                                </ContextMenu>
                            </TextBlock.ContextMenu>
                        </TextBlock>
                    </HierarchicalDataTemplate>
                </TreeView.Resources>
                <TreeViewItem 
                        Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Header}"
                        ItemsSource="{Binding}"
                        IsExpanded="True"
                        ItemTemplate="{StaticResource ProjectTemplate}">
                    <TreeViewItem.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Paste_Header}" 
                                    InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Paste_InputGestureText}" />
                        </ContextMenu>
                    </TreeViewItem.ContextMenu>
                </TreeViewItem>
            </TreeView>

And here is my click event

    private void EditProject_Click(object sender, RoutedEventArgs e)
    {
        //how do I find the bound item of the node this context menu resides on?
    }

I am trying to write the EditProject_Click event for the ContextMenu MenuItem click event. When I click it, I want to be able to find the item Bound to the node that the context menu resides. How do I do that?


The answer

    private void EditProject_Click(object sender, RoutedEventArgs e)
    {
        Project project = (Project)((MenuItem)sender).DataContext;

        //Do stuff with the project here
    }

Also, if you want to get your ContextMenu to update it’s DataContext from the element the ContextMenu is suppose to be attached to, here is the syntax for that:

<ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
  • 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-16T14:17:36+00:00Added an answer on May 16, 2026 at 2:17 pm

    In the click event handler, would “(sender as FrameworkElement).DataContext” be what you’re looking for?

    EDIT:

    Sample code below.

    XAML:

    <Window x:Class="WpfApplication3.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:v="clr-namespace:WpfApplication3"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <DataTemplate x:Key="template">
                <TextBlock Text="{Binding}">
                    <TextBlock.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Test" Click="MenuItem_Click"/>
                        </ContextMenu>
                    </TextBlock.ContextMenu>
                </TextBlock>
            </DataTemplate>
        </Window.Resources>
        <DockPanel>
            <Button DockPanel.Dock="Top" Click="Button_Click">Change Context</Button>
    
            <TreeView x:Name="tv" ItemTemplate="{StaticResource template}"/>
    
        </DockPanel>
    
    </Window>
    

    Code-behind:

    
    public partial class MainWindow : Window
    {
        bool flag = false;
        public MainWindow()
        {
            InitializeComponent();
            List list = new List() { "A", "B", "C", "D" };
            this.tv.ItemsSource = list;
        }
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show((sender as MenuItem).DataContext.ToString());
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (flag)
            {
                List list = new List() { "A", "B", "C", "D" };
                this.tv.ItemsSource = list;
            }
            else
            {
                List list = new List() { 1, 2, 3, 4 };
                this.tv.ItemsSource = list;
            }
            flag = !flag;
        }
    
    }
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have YAML data that looks sort of like this, but ~150k of it:
I have a treeview with nodes like this: Foo (1234), and want to allow
I have an app written in XUL which contains a custom tree view that
I have a ListStore that is filtered and then sorted. It looks something like
I have this code in jQuery, that I want to reimplement with the prototype
I have this RewriteRule that works too well :-) RewriteRule ^([^/]*)/$ /script.html?id=$1 [L] The
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
I have this idea for a free backup application. The largest problem I need
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM
I have this line in a javascript block in a page: res = foo('<%=

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.