WPF 创建右键菜单

主要用到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");
        }


你可能感兴趣的:(右键菜单)