3

I Want to Add or Change the context menu of treeview of wpf in my C# code.how can I do it???

ahmadali shafiee
  • 4,350
  • 12
  • 56
  • 91

1 Answers1

3

Why you need that? Perhaps it would be better to do it in an other way without affecting a code behind? I preffer when View and logic is decoupled, basically this is MVVM approach I would suggest to follow.

Anyway you can do it by adding Initialized event handler and than handing it in code behind:

XAML:

<TreeView ... />
  <TreeView.ContextMenu Initialized="OnTreeViewMenuInitialized">
    <MenuItem Header="Add" Command="{Binding AddCommand}" />
  </TreeView.ContextMenu>
</TreeView>

Code behind:

private void OnTreeViewMenuInitialized(object sender, EventArgs e)
{
     var contextMenu = (ContextMenu)sender;
     ...
}
sll
  • 61,540
  • 22
  • 104
  • 156