Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save LiamKarlMitchell/d66b34593ba51ceeefae52c7718739ac to your computer and use it in GitHub Desktop.
Save LiamKarlMitchell/d66b34593ba51ceeefae52c7718739ac to your computer and use it in GitHub Desktop.
Useful C# XAML
// Note, may have to use System.Windows.Controls
// In your method.
private void someTreeCm_SomeCommand(object sender, RoutedEventArgs e)
{
CustomTreeItem item = ((sender as MenuItem).DataContext) as CustomTreeItem;
}
// In your XAML
<TreeView x:Name="someTree">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Items}">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Name}" />
<StackPanel.ContextMenu>
<ContextMenu>
<MenuItem Header="SomeCommand" Click="someTreeCm_SomeCommand" />
</ContextMenu>
</StackPanel.ContextMenu>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment