欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WPF右键弹出菜单(ContextMenu的应用)

程序员文章站 2022-07-13 21:47:54
...
例子1:
<RichTextBox>
    <RichTextBox.ContextMenu>
        <ContextMenu>
            <MenuItem Command="Cut">
                <MenuItem.Icon>
                    <Image Source="Images/cut.png" />
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Command="Copy">
                <MenuItem.Icon>
                    <Image Source="Images/copy.png" />
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Command="Paste">
                <MenuItem.Icon>
                    <Image Source="Images/paste.png" />
                </MenuItem.Icon>
            </MenuItem>
         <MenuItem header="111111"  click=”xxxxx”>  </MenuItem>

        </ContextMenu>
    </RichTextBox.ContextMenu></RichTextBox>

例子2:
<DataTemplate>
   <Grid Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}">
      <Grid.ContextMenu>
          <ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
             <MenuItem Content="Cut" Command="{Binding CutCommand}" />
             <MenuItem Content="Copy" Command="{Binding CopyCommand}" />
             <MenuItem Content="Paste" Command="{Binding PasteCommand}" />
          </ContextMenu>
      </Grid.ContextMenu>
   </Grid>
</DataTemplate>

例子3: 点击按钮,出现菜单
<Window x:Class="WpfTutorialSamples.Common_interface_controls.ContextMenuManuallyInvokedSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ContextMenuManuallyInvokedSample" Height="250" Width="250">
    <Window.Resources>
        <ContextMenu x:Key="cmButton">  
            <MenuItem Header="Menu item 1" />
            <MenuItem Header="Menu item 2" />
            <Separator />
            <MenuItem Header="Menu item 3" />
        </ContextMenu>
    </Window.Resources>
    <Grid>
        <Button Content="Click me!" VerticalAlignment="Center" HorizontalAlignment="Center" Click="Button_Click" />
    </Grid>
</Window>
using System;
using System.Windows;
using System.Windows.Controls;

namespace WpfTutorialSamples.Common_interface_controls
{
    public partial class ContextMenuManuallyInvokedSample : Window
    {
        public ContextMenuManuallyInvokedSample()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ContextMenu cm = this.FindResource("cmButton") as ContextMenu;
            cm.PlacementTarget = sender as Button;
            cm.IsOpen = true;
        }
    }
}

效果如下图:
WPF右键弹出菜单(ContextMenu的应用)

例子4: 绑定listbox中的元素
<ListBox   Name="ListboxDisplay" Height="220" Margin="16,213,351,0" VerticalAlignment="Top">
<ListBox.ContextMenu>
<ContextMenu DataContext="Binding ElementName=ListboxDisplay, Path=SelectedItems">
    <MenuItem  Header="write epc" Click="menu_write_epc" ToolTip="" ></MenuItem>
    <MenuItem  Header="write tid" Click="menu_write_tid"  ToolTip=""></MenuItem>
    <MenuItem  Header="write user" Click="menu_write_user" ToolTip=""></MenuItem>
    <MenuItem  Header="lock" Click="menu_lock" ToolTip=""></MenuItem>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>

WPF右键弹出菜单(ContextMenu的应用)