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.
(treeViewItem.DataContext as ItemViewModel).Text
will do? Who will be changing Text and why?IsSelected
field forTreeViewItem
. When aIsSelected
is called on an item in my HierarchicalTree, I have theTreeViewItem
trigger the owner of the TreeView to iterate through all of the hosted items to find the one with the property bound toIsSelected
set to true. It's a workaround but it's possible to keep track of the current selection.