c# - 直到右键单击才应用上下文菜单样式

标签 c# .net wpf

我在 WPF (.NET 4.0) 中的一个控件的工具栏上的按钮上附加了一个上下文菜单。上下文菜单在定义上下文菜单的 XAML 中分配了一个样式。如果按钮尚未打开,则左键单击该按钮会打开该按钮的上下文菜单。

这是相关的 XAML:

<Button x:Name="fileButton" Foreground="White" Margin="7, 0, -3, 0" VerticalAlignment="Stretch" MaxHeight="70" MaxWidth="78" MinHeight="55" MinWidth="62" Style="{DynamicResource ImageButton}" utils:WpfImageUtil.Image="{StaticResource fileButton}" Template="{DynamicResource GlassButton}" Content="File" Visibility="Visible" Click="fileButton_Click">
    <Button.ContextMenu>
        <ContextMenu Style="{DynamicResource ContextMenuStyle}">
            <MenuItem x:Name="saveMenuItem" Header="Save" Click="saveMenuItem_Click" Style="{DynamicResource MenuItemStyle}" />
            <MenuItem x:Name="saveDrawingMenuItem" Header="Save Drawing" Click="saveMenuItem_Click" Style="{DynamicResource MenuItemStyle}" />
            <MenuItem x:Name="openMenuItem" Header="Open" Style="{DynamicResource MenuItemStyle}">
                <MenuItem x:Name="openFromFile" Header="From File" Style="{DynamicResource MenuItemStyle}" />
                <MenuItem x:Name="openFromDesktop" Header="From Desktop" Style="{DynamicResource MenuItemStyle}" />
            </MenuItem>
            <MenuItem x:Name="iconsMenuItem" Header="Icons" ItemsSource="{Binding}" Style="{DynamicResource MenuItemStyle}"/>
            <MenuItem x:Name="prefsMenuItem" Header="Preferences" Style="{DynamicResource MenuItemStyle}"/>
        </ContextMenu>
    </Button.ContextMenu>
</Button>

ContextMenuStyle 在正确引用的资源字典中定义。

当用左键单击打开上下文菜单时,我定义的样式没有应用到菜单,如下所示:

The style isn't applied on left click.

但是,如果用户右键单击并以传统方式打开上下文菜单,则会按预期应用样式:

The style is correctly applied on the first right click.

之后,左键单击按钮将正确显示样式:

After the right click, left click is styled properly.

一段时间以来,我一直在努力解决这个问题,但一直无法找出导致此问题发生的任何原因。这对我来说似乎是某种错误,但我不完全确定。我也不知道当右键单击控件时在较低级别会发生什么会导致正确应用样式。

最佳答案

您应该在代码中分配 ContextMenu Style 属性(FindResource 方法 msdn ):

private void fileButton_Click(object sender, RoutedEventArgs e)
{
    if (fileButton.ContextMenu.Style == null)
        fileButton.ContextMenu.Style = this.FindResource("ContextMenuStyle") as Style;
    fileButton.ContextMenu.IsOpen = true;
}

上下文菜单概述 ( http://msdn.microsoft.com/en-US/library/ms742558.aspx )

A ContextMenu is attached to a specific control. The ContextMenu element enables you to present users with a list of items that specify commands or options that are associated with a particular control, for example, a Button. Users right-click the control to make the menu appear. ...

当您右键单击控件时,样式将应用于 ContextMenu。因此,如果你想在代码中打开 ContextMenu,你应该检查 style 是否等于 null,如果为真,你应该分配适当的样式。

关于c# - 直到右键单击才应用上下文菜单样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19574433/

相关文章:

c# - TLS 之上的 p2p 协议(protocol),同步中间证书

c# - 找不到类型或命名空间名称 'DbEntityEntry<TEntity>'

c# - 如何使用 LINQ Contains() 查找枚举列表?

c# - 来自列表的 WPF Listview 数据绑定(bind)

c# - 如何使用 DryIoc 而不是 Unity 在 Prism 中配置模块目录?

wpf - 在名称范围内找不到名称 ---在数据网格中显示动画时出错

c# - 如何创建已在后端 HTML 表和 ASP.NET 中的 div 中创建的 onserverclick 事件

c# - 证书 - 什么是 KSP 和 CSP

c# - MSDN 帮助作为 XML 或 JSON 数据而不是 HTML 页面

c# - 基于多个 bool 条件的返回值