如何像默认上下文菜单一样为 ScrollBar 的自定义上下文菜单获取默认本地化?

Posted

技术标签:

【中文标题】如何像默认上下文菜单一样为 ScrollBar 的自定义上下文菜单获取默认本地化?【英文标题】:How to get default localization for custom context menu of ScrollBar like the default context menu? 【发布时间】:2021-05-07 00:25:20 【问题描述】:

按照这个问题,我已经成功地为滚动条控件创建了一个完全可用的上下文菜单:WPF - How to replace the scrollbar ContextMenu

问题是新的上下文菜单总是英文的,而原来的上下文菜单是自动翻译的(在我的例子中是意大利语)。

我知道我可以使用 ApplicationCommands 来自动翻译菜单(例如 CutCopyPaste 在文本框中),但我找不到任何滚动条命令。

这里有一份上下文菜单供参考:

<ContextMenu x:Key="VScrollBarContextMenu" x:Shared="true">
    <MenuItem Header="Scroll _Here" Command="ScrollBar.ScrollHereCommand" CommandTarget="Binding Path=PlacementTarget, RelativeSource=RelativeSource FindAncestor, AncestorType=x:Type ContextMenu" />
    <Separator/>
    <MenuItem Header="_Top" Command="ScrollBar.ScrollToTopCommand" CommandTarget="Binding Path=PlacementTarget, RelativeSource=RelativeSource FindAncestor, AncestorType=x:Type ContextMenu" />
    <MenuItem Header="_Bottom" Command="ScrollBar.ScrollToBottomCommand" CommandTarget="Binding Path=PlacementTarget, RelativeSource=RelativeSource FindAncestor, AncestorType=x:Type ContextMenu" />
    <Separator/>
    <MenuItem Header="Page _Up" Command="ScrollBar.PageUpCommand" CommandTarget="Binding Path=PlacementTarget, RelativeSource=RelativeSource FindAncestor, AncestorType=x:Type ContextMenu" />
    <MenuItem Header="Page _Down" Command="ScrollBar.PageDownCommand" CommandTarget="Binding Path=PlacementTarget, RelativeSource=RelativeSource FindAncestor, AncestorType=x:Type ContextMenu" />
    <Separator/>
    <MenuItem Header="Scroll U_p" Command="ScrollBar.LineUpCommand" CommandTarget="Binding Path=PlacementTarget, RelativeSource=RelativeSource FindAncestor, AncestorType=x:Type ContextMenu" />
    <MenuItem Header="Scroll Dow_n" Command="ScrollBar.LineDownCommand" CommandTarget="Binding Path=PlacementTarget, RelativeSource=RelativeSource FindAncestor, AncestorType=x:Type ContextMenu" />
</ContextMenu>

只需将菜单放在样式中即可。

<Style x:Key="x:Type ScrollBar" TargetType="x:Type ScrollBar">
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Style.Triggers>
        <Trigger Property="Orientation" Value="Horizontal">
            <Setter Property="Width" Value="Auto"/>
            <Setter Property="Height" Value="18"/>
            <Setter Property="ContextMenu" Value="DynamicResource HScrollBarContextMenu"/>
            <Setter Property="Template" Value="StaticResource HorizontalScrollBar"/>
        </Trigger>
        <Trigger Property="Orientation" Value="Vertical">
            <Setter Property="Width" Value="18"/>
            <Setter Property="Height" Value="Auto"/>
            <Setter Property="ContextMenu" Value="DynamicResource VScrollBarContextMenu"/>
            <Setter Property="Template" Value="StaticResource VerticalScrollBar"/>
        </Trigger>
    </Style.Triggers>
</Style>

原始菜单

重做的菜单

【问题讨论】:

【参考方案1】:

默认ContextMenuMenuItemHeader 值来自语言包中的resources,您可以在source code 中看到:

private static ContextMenu VerticalContextMenu

    get
    
        ContextMenu verticalContextMenu = new ContextMenu();
        verticalContextMenu.Items.Add(CreateMenuItem(SRID.ScrollBar_ContextMenu_ScrollHere, "ScrollHere", ScrollBar.ScrollHereCommand));
        verticalContextMenu.Items.Add(new Separator());
        verticalContextMenu.Items.Add(CreateMenuItem(SRID.ScrollBar_ContextMenu_Top, "Top", ScrollBar.ScrollToTopCommand));
        verticalContextMenu.Items.Add(CreateMenuItem(SRID.ScrollBar_ContextMenu_Bottom, "Bottom", ScrollBar.ScrollToBottomCommand));
        verticalContextMenu.Items.Add(new Separator());
        verticalContextMenu.Items.Add(CreateMenuItem(SRID.ScrollBar_ContextMenu_PageUp, "PageUp", ScrollBar.PageUpCommand));
        verticalContextMenu.Items.Add(CreateMenuItem(SRID.ScrollBar_ContextMenu_PageDown, "PageDown", ScrollBar.PageDownCommand));
        verticalContextMenu.Items.Add(new Separator());
        verticalContextMenu.Items.Add(CreateMenuItem(SRID.ScrollBar_ContextMenu_ScrollUp, "ScrollUp", ScrollBar.LineUpCommand));
        verticalContextMenu.Items.Add(CreateMenuItem(SRID.ScrollBar_ContextMenu_ScrollDown, "ScrollDown", ScrollBar.LineDownCommand));
        return verticalContextMenu;
    

这些是内部的,因此您无法绑定到它们。如果您创建自己的自定义ContextMenu,则需要自己定义翻译。

【讨论】:

以上是关于如何像默认上下文菜单一样为 ScrollBar 的自定义上下文菜单获取默认本地化?的主要内容,如果未能解决你的问题,请参考以下文章

请教一下高手,怎么改变select下拉菜单的样式。右边那个默认的箭头很丑,怎么换成一个好看的箭头。

win11 alt+tab切换窗口后,默认激活了窗口的菜单栏,能否改成win10一样,默认激活窗口原来的控件?

如何像chrome一样拆分菜单

如何在颤动中从 ListView 中删除默认的 ScrollBar?

如何关闭material UI上下文菜单,而不显示默认的上下文菜单?

如何像移动应用一样创建侧边导航菜单?