2

I want to define a double click even on a TreeView so that I will be able to know which item in the TreeView was selected and to get his title. The way I try to get it's title gets me "MyProject.MenuItem". How can I refer to the selected item on the tree, make sure it's not the root, and get it's title? What I did:

<TreeView Name="trvMenu" HorizontalAlignment="Left" Height="312" VerticalAlignment="Top" Width="200" MouseDoubleClick="TreeView_MouseDoubleClick" >
        <TreeView.ItemTemplate>
               <HierarchicalDataTemplate DataType="{x:Type local:MenuItem}" ItemsSource="{Binding Items}">
                     <TextBlock Text="{Binding Title}" />
               </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
</TreeView>

The MessageBox shows "MyProject.MenuItem", what I want to do is not show a messagebox, but to get the title of the selected treeview item, after checking it is not the root

private void TreeView_MouseDoubleClick(object sender, RoutedEventArgs e)
    {
        if (sender is TreeViewItem)
            if (!((TreeViewItem)sender).IsSelected)
                return;
        TreeViewItem tviSender = sender as TreeViewItem;
        MessageBox.Show(trvMenu.SelectedItem.ToString());
    }
Yona
  • 269
  • 1
  • 5
  • 18
  • So you need to access child elements of the TreeView in the code-behind? – Robert Langdon Jun 16 '14 at 06:28
  • http://stackoverflow.com/questions/24197633/recursively-collapse-all-child-nodes-of-parent-node-in-custom-usercontrol/24197901#24197901 – Sajeetharan Jun 16 '14 at 06:29
  • Yes, child elements are added in runtime, and I want to get their title in the code-behind. @RobertLangdon – Yona Jun 16 '14 at 06:38
  • I faced a similar type of problem some-time ago. Check out the link and let us know if it works for you. http://stackoverflow.com/questions/11826272/find-a-wpf-element-inside-datatemplate-in-the-code-behind – Robert Langdon Jun 16 '14 at 06:46
  • I don't understand how to use it :\ @RobertLangdon – Yona Jun 16 '14 at 06:57
  • You need to have a method to access the child elements and invoke it whenever needed. – Robert Langdon Jun 16 '14 at 07:10
  • But that's the problem, I don't know how to access the child elements @RobertLangdon – Yona Jun 16 '14 at 07:12
  • There is no problem at all. `trvMenu.SelectedItem` already gives you the selected item, which is an instance of your `MenuItem` class. Just don't call `ToString` on it. Instead, get its `Title` property. – Clemens Jun 16 '14 at 07:18
  • Check out Josh Smith's classic article on ['Simplifying the WPF TreeView...'](http://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode). Using a ViewModel class instead of code-behind to control the TreeView and access its state (e.g., SelectedItem) makes working with it way easier IMO. – EagleBeak Jun 16 '14 at 08:19

1 Answers1

1

Change your double click handler like shown below. Instead of calling ToString it accesses the Title property of your MenuItem item class.

private void TreeView_MouseDoubleClick(object sender, RoutedEventArgs e)
{
    var menuItem = trvMenu.SelectedItem as MyProject.MenuItem;

    if (menuItem != null)
    {
        MessageBox.Show(menuItem.Title);
    }
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • It doesn't recognize "Title" and doesn't give in to any casting >_ – Yona Jun 16 '14 at 07:42
  • ok, i did " MessageBox.Show(((MenuItem)trvMenu.SelectedItem).Title); " but now it doesn't even show the messagebox.. – Yona Jun 16 '14 at 07:45
  • Just put a breakpoint on MessaBox.Show line and inspect trvMenu.SelectedItem and make sure that it the object that you are expecting it to be – Krishna Jun 16 '14 at 07:56