WPF ContextMenu

一、ContextMenu

 

 如图,基本样式结构

<Style TargetType="{x:Type ContextMenu}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ContextMenu}">
                <Border x:Name="Border" Background="White">
                    <!--MenuItem 位置-->
                    <ItemsPresenter/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style TargetType="{x:Type MenuItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type MenuItem}">
                <Border x:Name="border" VerticalAlignment="Center" Padding="25 5" Background="Transparent">
                    <WrapPanel>
                        <!--Icon 内容-->
                        <!--<ContentPresenter ContentSource="Icon" HorizontalAlignment="Center" VerticalAlignment="Center" />-->
                        <!--文字内容-->
                        <ContentPresenter ContentSource="Header" HorizontalAlignment="Left" VerticalAlignment="Center"/>
                    </WrapPanel>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="#dfdddb" TargetName="border"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

 XAML 调用方式为:

<ListBox.ContextMenu>
    <ContextMenu>
        <MenuItem Header="置顶"></MenuItem>
        <MenuItem Header="设为已读" />
        <MenuItem Header="开启新消息提醒" />
        <MenuItem Header="在独立窗口打开" />
        <Separator Style="{StaticResource HorizontalSeparatorStyle}"/>
        <MenuItem Header="不显聊天" />
        <MenuItem Header="删除聊天" />
    </ContextMenu>
</ListBox.ContextMenu>

 绑定命令:
Command="{Binding cmd,Source={StaticResource dataContextProxy}}"

如果想传递附着的元素Button
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget}"

如果你想传递Button的父级元素
CommandParameter="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"

 

二、左键弹出操作菜单,并禁用右键菜单

1、在前台加入一个带菜单的Button,如下:

 

<Button Name="btnMenu" Width="50" Height="25" Content="菜单" Initialized="btnMenu_Initialized">
    <Button.ContextMenu>
        <ContextMenu Name="contextMenu">
            <MenuItem Header="复制"/>
            <MenuItem Header="粘贴"/>
            <MenuItem Header="剪切"/>
            <MenuItem Header="删除"/>
        </ContextMenu>
    </Button.ContextMenu>
</Button>

 

2、在后台,实现Initialized="btnMenu_Initialized",初始化右键菜单为null

 private void btnMenu_Initialized(object sender, EventArgs e)
 {
     //设置右键菜单为null
     this.btnMenu.ContextMenu = null;
 }

3、设置单击鼠标左键弹出菜单,如下:

private void btnMenu_Click(object sender, RoutedEventArgs e)
{
    //目标
    this.contextMenu.PlacementTarget = this.btnMenu;
    //位置
    this.contextMenu.Placement = PlacementMode.Top;
    //显示菜单
    this.contextMenu.IsOpen = true;
}

  

 命令绑定到ContextMenu(在ListBox中的ListBoxItem上) 不起作用解决方案

参考:https://www.codenong.com/19447795/

 

参考:https://www.cnblogs.com/zsmhhfy/archive/2013/03/18/2965755.html

posted @ 2021-05-28 16:20  microsoft-zhcn  阅读(2093)  评论(0编辑  收藏  举报