WPF 创建右键菜单

来源:互联网 发布:迪奥淘宝旗舰店 编辑:程序博客网 时间:2024/05/21 17:31

主要用到ContextMenu类,对于任何的控件都可以进行对ContextMenu属性的操作进行设置右键菜单的功能.

xaml代码如下:

<Window x:Class="右键菜单.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="MainWindow" Height="350" Width="525">    <Grid>        <ListView>            <ListView.ContextMenu>                <ContextMenu Name="gridCM" StaysOpen="True">                    <MenuItem Header="File" Click="MenuItem_Click_1">                        <MenuItem.Icon>                            <Image Source="2.ico"></Image>                        </MenuItem.Icon>                    </MenuItem>                    <MenuItem Header="Save"/>                </ContextMenu>            </ListView.ContextMenu>        </ListView>    </Grid></Window>

菜单响应事件:

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)        {            MessageBox.Show("haha");        }


原创粉丝点击