WPF - How to replace the ContextMenu scrollbar

I am trying to replace ContextMenu for ScrollBars and I wrote 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>

      

The ContextMenu gets a lot, but it acts weird. Initially, all menu items are disabled. When you scroll the scrollbar, they all activate, except for the ScrollHere command, which is permanently disabled. Also when you click an option, that is, the Page Up option, it only works when the hosting control of the scrollbar is focused (it doesn't focus automatically). Does anyone know how to solve these problems?

EDIT: My guess is that, by default, the ContextMenu handles the event Opening

and focuses the control by default and also stores the location of the point where the mouse was clicked somewhere. But how can I put this functionality in a XAML file ???

+3


source to share


1 answer


OK. This 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 a target command ...

+2


source







All Articles