Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context Menu for XAML Treeviewitem (Distinguished by different attributes)

Tags:

In XAML, how do you define a context menu for treeviewitems that are distinguished by different attributes?

like image 817
Mr. T. Avatar asked Sep 09 '09 10:09

Mr. T.


1 Answers

XAML

<TreeView Name="SolutionTree"  BorderThickness="0" SelectedItemChanged="SolutionTree_SelectedItemChanged"  >   <TreeView.Resources>     <ContextMenu x:Key ="SolutionContext"  StaysOpen="true">       <MenuItem Header="Add..." Click="AddFilesToFolder_Click"/>       <MenuItem Header="Rename"/>     </ContextMenu>     <ContextMenu x:Key="FolderContext"  StaysOpen="true">       <MenuItem Header="Add..." Click="AddFilesToFolder_Click"/>       <MenuItem Header="Rename"/>       <MenuItem Header="Remove"/>       <Separator/>       <MenuItem Header="Copy"/>       <MenuItem Header="Cut"/>       <MenuItem Header="Paste"/>       <MenuItem Header="Move"/>     </ContextMenu>   </TreeView.Resources> </TreeView> 

C-sharp

private void SolutionTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) {     TreeViewItem SelectedItem = SolutionTree.SelectedItem as TreeViewItem;     switch (SelectedItem.Tag.ToString())     {         case "Solution":             SolutionTree.ContextMenu = SolutionTree.Resources["SolutionContext"] as System.Windows.Controls.ContextMenu;             break;         case "Folder":             SolutionTree.ContextMenu = SolutionTree.Resources["FolderContext"] as System.Windows.Controls.ContextMenu;             break;     } } 
like image 200
Mr. T. Avatar answered Oct 07 '22 14:10

Mr. T.