WPF Treeview Context Menu

Introduction

This blog provides code examples of how to add the WPF context menu into WPF Treeview using XAML code and at run time using C# code.

WPF Treeview Context Menu using XAML

The following below XAML code adds the WPF context menu into WPF Treeview. Which contains three menus (Add, Edit and Delete)

 <TreeView>  
       <TreeView.ContextMenu>  
         <ContextMenu>  
           <MenuItem Header="_Add" />  
           <MenuItem Header="_Edit"/>  
           <MenuItem Header="_Delete"/>  
         </ContextMenu>  
       </TreeView.ContextMenu>  
     </TreeView>  

WPF Treeview Context Menu

WPF Treeview Context Menu using C#

The following below code adds the WPF context menu into WPF Treeview using C#. you can use this code to add WPF context menu into WPF Treeview at run time.

 this.tvcontextmenu.ItemsSource = emps;  
   
       ContextMenu contextMenu = new ContextMenu();  
   
       MenuItem menuItem_add = new MenuItem();  
       menuItem_add.Header = "_Add";  
       menuItem_add.Command = ApplicationCommands.New;  
   
       contextMenu.Items.Add(menuItem_add);  
   
       MenuItem menuItem_edit = new MenuItem();  
       menuItem_edit.Header = "_Edit";  
       menuItem_edit.Command = ApplicationCommands.Copy;  
   
       contextMenu.Items.Add(menuItem_edit);  
   
       MenuItem menuItem_delete = new MenuItem();  
       menuItem_delete.Header = "_Delete";  
       menuItem_delete.Command = ApplicationCommands.Delete;  
   
       contextMenu.Items.Add(menuItem_delete);  
   
       this.tvcontextmenu.ContextMenu = contextMenu;  

WPF Treeview Context Menu

Summary

In the above code examples, we learn how to add WPF Context menu int WPF Treeview using XAML code and C# code. I hope you have enjoyed it a lot.

Thanks

Kailash Chandra Behera

An IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies. Demonstrated expertise in delivering all phases of project development—from initiation to closure—while aligning with business objectives to drive process improvements, competitive advantage, and measurable bottom-line gains. Proven ability to work independently and manage multiple projects successfully. Committed to the efficient and effective development of projects in fast-paced, deadline-driven environments. Skills: Proficient in designing and developing applications using various Microsoft technologies. Total IT Experience: 13+ years

Previous Post Next Post

نموذج الاتصال