2021-08-14 WPF控件专题 ContextMenu 控件详解
【摘要】
1.ContextMenu 控件介绍
简介:父类:MenuBase MenuItem (HeaderedItemsControl) ItemsControl 特定于某个元素之上的功能菜单。(右键菜单) ...
1.ContextMenu 控件介绍
简介:父类:MenuBase MenuItem (HeaderedItemsControl) ItemsControl
特定于某个元素之上的功能菜单。(右键菜单) 上下文菜单
属性:HorizontalOffset、VerticalOffset 右键菜单控件相对于点击位置的水平、垂直距离点
Label(右键菜单的目标元素)
快捷键响应:与命令或事件处理程序关联起来
应用:不独立存在,依赖于某个元素(目标元素)
2.具体案例
<Window x:Class="WpfAppTest.ContextMenuWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfAppTest"
mc:Ignorable="d"
Title="ContextMenuWindow" Height="450" Width="800">
<Grid>
<Label Name="lbl" Content="用户管理" HorizontalAlignment="Left" Margin="149,23,0,0" VerticalAlignment="Top" Height="33" Width="73" BorderBrush="Blue" BorderThickness="1" MouseLeftButtonDown="Lbl_MouseLeftButtonDown" ContextMenuService.Placement="RelativePoint" >
<Label.ContextMenu>
<ContextMenu Name="contextMenu" HasDropShadow="True" HorizontalOffset="20" VerticalOffset="20" >
<MenuItem Header="打开页面"/>
<MenuItem Header="操作">
<MenuItem Header="复制" InputGestureText="Ctrl+C"/>
<MenuItem Header="剪切"/>
<MenuItem Header="删除"/>
</MenuItem>
</ContextMenu>
</Label.ContextMenu>
</Label>
</Grid>
</Window>
/// <summary>
///左键打开上下文菜单
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Lbl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
contextMenu.PlacementTarget = lbl;
contextMenu.IsOpen = true;
}
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/119705013
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)