2021-08-23 WPF控件专题 ContextMenu 控件详解

爱被打了一巴掌 2022-09-07 09:53 444阅读 0赞

在这里插入图片描述

1.ContextMenu 控件介绍

简介:父类:MenuBase MenuItem (HeaderedItemsControl) ItemsControl
特定于某个元素之上的功能菜单。(右键菜单) 上下文菜单

属性:HorizontalOffset、VerticalOffset 右键菜单控件相对于点击位置的水平、垂直距离点
Label(右键菜单的目标元素)

快捷键响应:与命令或事件处理程序关联起来

应用:不独立存在,依赖于某个元素(目标元素)

2.具体案例

  1. <Window x:Class="WpfAppTest.ContextMenuWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:WpfAppTest"
  7. mc:Ignorable="d"
  8. Title="ContextMenuWindow" Height="450" Width="800">
  9. <Grid>
  10. <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" >
  11. <Label.ContextMenu>
  12. <ContextMenu Name="contextMenu" HasDropShadow="True" HorizontalOffset="20" VerticalOffset="20" >
  13. <MenuItem Header="打开页面"/>
  14. <MenuItem Header="操作">
  15. <MenuItem Header="复制" InputGestureText="Ctrl+C"/>
  16. <MenuItem Header="剪切"/>
  17. <MenuItem Header="删除"/>
  18. </MenuItem>
  19. </ContextMenu>
  20. </Label.ContextMenu>
  21. </Label>
  22. </Grid>
  23. </Window>
  24. /// <summary>
  25. ///左键打开上下文菜单
  26. /// </summary>
  27. /// <param name="sender"></param>
  28. /// <param name="e"></param>
  29. private void Lbl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  30. {
  31. contextMenu.PlacementTarget = lbl;
  32. contextMenu.IsOpen = true;
  33. }

发表评论

表情:
评论列表 (有 0 条评论,444人围观)

还没有评论,来说两句吧...

相关阅读