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>
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?
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With