如何像默认上下文菜单一样为 ScrollBar 的自定义上下文菜单获取默认本地化?
How to get default localization for custom context menu of ScrollBar like the default context menu?
我已经通过以下问题成功地为滚动条控件创建了一个完全可用的上下文菜单:WPF - How to replace the scrollbar ContextMenu
问题是新的上下文菜单总是英文的,而原始的上下文菜单是自动翻译的(在我的例子中是意大利语)。
我知道我可以使用 ApplicationCommands 自动翻译菜单(例如文本框中的 Cut
、Copy
、Paste
),但我找不到任何命令用于滚动条。
这里是上下文菜单的副本以供参考:
<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>
原版菜单
重新设计的菜单
默认 ContextMenu
的 MenuItem
从 resources in the language packs which you can see in the source code:
中获取其 Header
值
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
,则需要自己定义翻译。
我已经通过以下问题成功地为滚动条控件创建了一个完全可用的上下文菜单:WPF - How to replace the scrollbar ContextMenu
问题是新的上下文菜单总是英文的,而原始的上下文菜单是自动翻译的(在我的例子中是意大利语)。
我知道我可以使用 ApplicationCommands 自动翻译菜单(例如文本框中的 Cut
、Copy
、Paste
),但我找不到任何命令用于滚动条。
这里是上下文菜单的副本以供参考:
<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>
原版菜单
重新设计的菜单
默认 ContextMenu
的 MenuItem
从 resources in the language packs which you can see in the source code:
Header
值
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
,则需要自己定义翻译。