简体   繁体   中英

How i can add ContextMenu in ListView (want to add “Modify” operation when someone do right click on ListView item ) in C# WPF

I am trying to implement right click operation on ListView item and want to show some function on right click like "Modify".

                                              Grid.RowSpan="3" Grid.ColumnSpan="2"  >
                                        <ListView.View>
                                            <GridView>
                                                <GridViewColumn Width="100" Header="Book Title" DisplayMemberBinding="{Binding Name}"/>
                                                <GridViewColumn Width="80" Header="Book Price" DisplayMemberBinding="{Binding RSSI}"/>
                                            </GridView>
                                        </ListView.View>
                                    </ListView>

You must create a context menu in code-behind once the user click on the right mouse's button using PreviewMouseRightButtonUp event so :

  • Create a name proprety for your ListView example: <ListView x:Name="test">

  • In your constructor initialize the event listner for your ListView after calling the InitializeComponent method like this :

     public your_constructor() { InitializeComponent(); /* Your codes here ... */ test.PreviewMouseRightButtonUp += delegate { System.Windows.Controls.ContextMenu contextMenu = new System.Windows.Controls.ContextMenu(); System.Windows.Controls.MenuItem modify_menu = new System.Windows.Controls.MenuItem(); modify_menu.Header = "Modify"; modify_menu.Click += delegate { //your codes here once the user click on "Modify" ... }; }; } 

    Happy Coding

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM