我正在尝试替换 ScrollBars 的 ContextMenu 并且我已经编写了以下代码:
<ContextMenu x:Key="ScrollBarContextMenu" x:Shared="True">
<MenuItem Header="Scroll _Here" Name="SH" Command="ScrollBar.ScrollHereCommand" />
<Separator/>
<MenuItem Header="_Top" Name="T" Command="ScrollBar.ScrollToTopCommand" />
<MenuItem Header="_Bottom" Name="B" Command="ScrollBar.ScrollToBottomCommand" />
<Separator/>
<MenuItem Header="Page _Up" Name="PU" Command="ScrollBar.PageUpCommand" />
<MenuItem Header="Page _Down" Name="PD" Command="ScrollBar.PageDownCommand" />
<Separator/>
<MenuItem Header="Scroll U_p" Name="SU" Command="ScrollBar.LineUpCommand" />
<MenuItem Header="Scroll Dow_n" Name="SD" Command="ScrollBar.LineDownCommand" />
</ContextMenu>
<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="ContextMenu" Value="{DynamicResource ScrollBarContextMenu}"/>
<Style.Triggers>
<Trigger Property="Orientation" Value="Horizontal">
<Setter Property="Width" Value="Auto"/>
<Setter Property="Height" Value="18" />
<Setter Property="Template" Value="{StaticResource HorizontalScrollBar}" />
</Trigger>
<Trigger Property="Orientation" Value="Vertical">
<Setter Property="Width" Value="18"/>
<Setter Property="Height" Value="Auto" />
<Setter Property="Template" Value="{StaticResource VerticalScrollBar}" />
</Trigger>
</Style.Triggers>
</Style>
ContextMenu 已设置,但它的行为很奇怪。最初它的所有菜单项都被禁用。当您滚动滚动条时,它们都会启用,除了永远禁用的 ScrollHere 命令。此外,当单击一个选项时,即“Page Up”选项,它仅在承载滚动条的控件获得焦点时才起作用(它不会自动获得焦点)。有谁知道如何解决这些问题?
编辑:
我的猜测是,也许默认的 ContextMenu 处理Opening
事件并聚焦控件,加上它存储某处用鼠标单击的点的位置。但是我怎样才能把这个功能放在 XAML 文件中???