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:
I was missing the command target…