3

我正在尝试替换 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 文件中???

4

1 回答 1

3

好的。这是您的操作方法:

        <ContextMenu x:Key="VScrollBarContextMenu" x:Shared="true">
            <MenuItem Header="{DynamicResource ScrollHere}" Command="ScrollBar.ScrollHereCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"/>
            <Separator/>
            <MenuItem Header="{DynamicResource ScrollTop}" Command="ScrollBar.ScrollToTopCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
            <MenuItem Header="{DynamicResource ScrollBottom}" Command="ScrollBar.ScrollToBottomCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
            <Separator/>
            <MenuItem Header="{DynamicResource ScrollPageUp}" Command="ScrollBar.PageUpCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
            <MenuItem Header="{DynamicResource ScrollPageDown}" Command="ScrollBar.PageDownCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
            <Separator/>
            <MenuItem Header="{DynamicResource ScrollUp}" Command="ScrollBar.LineUpCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
            <MenuItem Header="{DynamicResource ScrollDown}" Command="ScrollBar.LineDownCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
        </ContextMenu>

        <ContextMenu x:Key="HScrollBarContextMenu" x:Shared="true">
            <MenuItem Header="{DynamicResource ScrollHere}" Command="ScrollBar.ScrollHereCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"/>
            <Separator/>
            <MenuItem Header="{DynamicResource ScrollLeftEnd}" Command="ScrollBar.ScrollToLeftEndCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
            <MenuItem Header="{DynamicResource ScrollRightEnd}" Command="ScrollBar.ScrollToRightEndCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
            <Separator/>
            <MenuItem Header="{DynamicResource ScrollPageLeft}" Command="ScrollBar.PageLeftCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
            <MenuItem Header="{DynamicResource ScrollPageRight}" Command="ScrollBar.PageRightCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
            <Separator/>
            <MenuItem Header="{DynamicResource ScrollLeft}" Command="ScrollBar.LineLeftCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
            <MenuItem Header="{DynamicResource ScrollRight}" Command="ScrollBar.LineRightCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
        </ContextMenu>

我错过了命令目标...

于 2013-02-04T21:18:18.060 回答