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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:24:44+00:00 2026-05-29T06:24:44+00:00

I think it will be very hard for you to read the code but

  • 0

I think it will be very hard for you to read the code but I’ll try to do my best !

Here is my xaml code :

<TreeView x:Name="stateMachinesView"
          DockPanel.Dock="Top"
          SelectedItemChanged="item_Selected"
          HorizontalAlignment="Stretch"
          VerticalAlignment="Stretch"
          ScrollViewer.HorizontalScrollBarVisibility="Disabled"
          BorderThickness="0">
<TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Value}">
        <HierarchicalDataTemplate.ItemTemplate>
            <DataTemplate>
                <DockPanel>
                    <DockPanel.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Create Thumbnail" 
                                      Click="MenuItemCreate_Click"/>
                        </ContextMenu>
                    </DockPanel.ContextMenu>
                    <Image>
                        <Image.Style>
                            <Style TargetType="Image">
                                <Style.Setters>
                                    <Setter Property="Source"
                                            Value="Resources\state.png"/>
                                </Style.Setters>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Item2}"
                                                 Value="true">
                                        <Setter Property="Source"
                                                Value="Resources\state_init.png"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>
                    <TextBlock>
                        <TextBlock.Text>
                            <Binding Path="Item1"/>
                        </TextBlock.Text>
                    </TextBlock>
                </DockPanel>
            </DataTemplate>
        </HierarchicalDataTemplate.ItemTemplate>
        <DockPanel>
            <Image DockPanel.Dock="Left"
                   Source="Resources\state_machine.png"/>
                <TextBlock Text="{Binding Key}"/>
        </DockPanel>
    </HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>

The item source of this is a Dictionary<string, ObservableCollection<Tuple<string, bool>>>

Visually, I got something like this:

Screenshot

Now, when I click on my MenuItem I got this code:

private void MenuItemCreate_Click(object sender, RoutedEventArgs e)
{
    string stateName =
        ((sender as FrameworkElement).DataContext as Tuple<string, bool>).Item1;
}

Here I can access to State1_1 with the code above, but now I would like to access to SM1 the parent node !

I tried a lot of things, the closest (to the solution) was this:

DependencyObject parent = VisualTreeHelper.GetParent(sender as DependencyObject);
while (!(parent is TreeViewItem))
    parent = VisualTreeHelper.GetParent(parent);

But it doesn’t work…

I am, too, thinking about a Template in XAML but im sure I can do it in the code-behind easily!

  • 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-29T06:24:44+00:00Added an answer on May 29, 2026 at 6:24 am

    ContextMenus are not on the same visual tree as the object they are used on. You have go up twice

    1. You need to get up to the ContextMenu, there you can get the TreeViewItem from the ContextMenu.PlacementTarget.
    2. Now you can go up that tree to the parent TreeViewItem.

    Of course it would be easier if you just have a reference to the parent in the data items themselves. Also you should not need to acces the TreeViewItems as you usually bind everything as necessarry.

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

Sidebar

Related Questions

I am learning LINQ and have a very simple question that I think will
I think this will be easy but I can't see how to do it!
I would like to do the following but I don't think this will work:
this is very hard to explain and give code based on it. So thank
I'm having a very hard time understanding the jQuery Form Plugin. I've read through
EDIT2 Looks like I can't read properly, disregard this post, I will try to
sorry for that title but it is very hard to explain in a few
Do you think that C# will support something like ??= operator? Instead of this:
I think even if we will not need interoperability between applications, and even we
I will start by saying that I don't think what I want can be

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.