-1

I've got in XAML

<TreeView x:Name="FavoritesTreeView" SelectedItemChanged="FavpritesTreeView_SelectedItemChanged" PreviewMouseRightButtonUp="FavpritesTreeView_PreviewMouseRightButtonUp">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate DataType="{x:Type self:Node}" ItemsSource="{Binding Items}">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding ImagePath}"/>
                <TextBlock x:Name="NodeTitle" Text="{Binding Title}" />
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

and in C#

private void FavpritesTreeView_PreviewMouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    // Select TreeView Node on right click before displaying ContextMenu
    // https://stackoverflow.com/questions/592373/select-treeview-node-on-right-click-before-displaying-contextmenu

    DependencyObject obj = e.OriginalSource as DependencyObject;
    TreeViewItem treeViewItem = GetDependencyObjectFromVisualTree(obj, typeof(TreeViewItem)) as TreeViewItem;

    if (treeViewItem != null)
    {
        // do someting
    }
}

How to get NodeTitle item to change its Text, for example?

Have tried different approaches but no success. I'm expecting to get help to resolve the problem.

4
  • Maybe this post could help? Commented Aug 28 at 9:08
  • 1
    It's not clear what you have at hand, but maybe (treeViewItem.DataContext as ItemViewModel).Text will do? Who will be changing Text and why?
    – Sinatr
    Commented Aug 28 at 9:29
  • I'm doing a kind of treeview editor. So I need to have possibility to rename a node, delete a node etc. Commented Aug 28 at 9:39
  • To keep track of this in one of my projects, I bound a property to the IsSelected field for TreeViewItem. When a IsSelected is called on an item in my HierarchicalTree, I have the TreeViewItem trigger the owner of the TreeView to iterate through all of the hosted items to find the one with the property bound to IsSelected set to true. It's a workaround but it's possible to keep track of the current selection.
    – bigcrazyal
    Commented Aug 28 at 20:58

1 Answer 1

-1

You can get it simply from Source property of MouseButtonEventArgs:

TreeViewItem treeViewItem = (TreeViewItem)e.Source;

Update

Inside the event e.Source has TreeViewItem type: enter image description here

And to get children of a item, you can use VisualTreeHelper class to get the children.

For example, using answer from this SO post:

public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
    if (obj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            var child = VisualTreeHelper.GetChild(obj, i);
            if (child is T)
            {
               return (T)child;
            }

            T childItem = FindVisualChild<T>(child);
            if (childItem != null) return childItem;
        }
    }
    return null;
}

And then use it like:

var textBox = FindVisualChild<TextBlock>((DependencyObject)e.Source);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.