如何替换滚动条ContextMenu

如何替换滚动条ContextMenu

本文介绍了WPF-如何替换滚动条ContextMenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将ScrollBars替换为ContextMenu,并且已经编写了以下代码:

I am trying to replace the ContextMenu for ScrollBars and I have written this code:

<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命令始终保持禁用状态外,所有其他功能均已启用.同样,当单击一个选项(即向上翻页"选项)时,它仅在承载滚动条的控件被聚焦时才起作用(它不会自动聚焦).有谁知道如何解决这些问题?

The ContextMenu gets set, but it acts weird. Initially all it's menu items are disabled. When you scroll the scroll bar they all get enabled except the ScrollHere command that stays disabled for ever. Also when clicking an option, i.e. the 'Page Up' option, it works only when the control hosting the scroll bar is focused (it doesn't get focused automatically). Does anyone know how to solve these problems?

我的猜测是,默认的ContextMenu可能处理 Opening 事件并集中控制该控件,此外它还存储了用鼠标单击的点的位置.但是如何将这个功能放在XAML文件中?

EDIT :My guess is that perhaps the default ContextMenu handles the Opening event and focuses the control, plus it stores somewhere the location of the point that was clicked with the mouse. But how can I put this functionality in a XAML file???

推荐答案

确定.这是您的操作方式:

OK. Here is how you do it:

        <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>

我错过了命令目标...

I was missing the command target...

这篇关于WPF-如何替换滚动条ContextMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 18:45