I'm not 100% sure what you are looking for here, but adding this code to your example will cause ScrollViewer to get focus when you click on the ScrollBar, drag and drop, etc. One more code is required for this solution.
<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
<EventSetter Event="PreviewMouseDown" Handler="scrollBar_PreviewMouseDown"/>
</Style>
And in code
void scrollBar_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
c_scrollViewer.Focus();
}
UPDATE
You may know how to add this to the resource dictionary to access it for several ScrollViewers, otherwise here's how you can do it.
. ScrollBarStyles.xaml.
, ScrollBarStyles.xaml.cs.
x: Class xaml, -
x:Class="YourNameSpace.ScrollBarStyles"
ScrollBarStyles.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="FocusScrollViewer.ScrollBarStyles">
<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
<EventSetter Event="PreviewMouseDown" Handler="scrollBar_PreviewMouseDown"/>
</Style>
</ResourceDictionary>
ScrollBarStyles.xaml.cs
public partial class ScrollBarStyles
{
public T GetVisualParent<T>(object childObject) where T : Visual
{
DependencyObject child = childObject as DependencyObject;
while ((child != null) && !(child is T))
{
child = VisualTreeHelper.GetParent(child);
}
return child as T;
}
void scrollBar_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
ScrollBar scrollBar = sender as ScrollBar;
ScrollViewer scrollViewer = GetVisualParent<ScrollViewer>(scrollBar);
scrollViewer.Focus();
}
}
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ScrollBarStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>