wpf中添加右击菜单

版权声明:biubiubiu https://blog.csdn.net/a_lllll/article/details/88909374

1.在XAML中window下添加菜单,代码格式如下

<Window.Resources>
        <ContextMenu x:Key="StucmButton">
            <MenuItem FontFamily="微软雅黑" Name="lookerStu" Header="查看学生信息" Click="lookerStu_Click"/>
            <MenuItem FontFamily="微软雅黑" Name="deleteStu" Header="删除学生" Click="deleteStu_Click"/>
        </ContextMenu>
    </Window.Resources>

2.右击事件里打开上面的cmButton菜单

		/// <summary>
        /// 右键获取选中的ID
        /// </summary>
        object sendery;
        /// <summary>
        /// 右键菜单的显示
        /// </summary>
        private void UserPanel_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
        	//右键获取到选中当前对象的信息(这里选中的是遍历出的StackPanel控件)
            sendery = (sender as StackPanel).Tag;
            //打开右键菜单
            ContextMenu cm = this.FindResource("StucmButton") as ContextMenu;
            cm.PlacementTarget = sender as StackPanel;
            cm.IsOpen = true;
        }

猜你喜欢

转载自blog.csdn.net/a_lllll/article/details/88909374
今日推荐