I Want to Add or Change the context menu of treeview of wpf in my C# code.how can I do it???
Asked
Active
Viewed 4,167 times
3
-
http://stackoverflow.com/questions/1398943/context-menu-for-xaml-treeviewitem – GazTheDestroyer Oct 28 '11 at 10:53
1 Answers
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
-
I Have a DLL File(Class Library) that Changes A TreeView To Show Some New Items.and I Can't Edit TreeView's Context Menu in XAML – ahmadali shafiee Oct 28 '11 at 11:36
-