How to get scrollbar in scrollviewer to focus scrollviewer on click?

Currently, with the scrollviewer, clicking on the scroll bar contained in it to pan the content around it will not focus the viewer (or something like that). I want this to be when you scroll, by dragging and dropping a scroll, or even clicking on the scroll bar, you will focus the parent (scroll view).

I did this somewhat by adding a ScrollChanged handler to scrollviewer and calling sender.focus (). Unforutalally ScrollChanged is called when scrollviewer is initialized, etc. While loading

I also tried installing event collectors that weren't lucky yet.

Ideally, I would like it to be in some style or something that allows me to apply it to all view scrolls throughout the application.

Edit: just for further clarification, if I use this xaml and click on the scroll bar, it will not become a blue background (gotfocus event). When I press the scroll button or the button, it will be.

<Window x:Class="GotFocusProblem.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid Margin="20" x:Name="grid">
            <Grid.Background>
                <SolidColorBrush x:Name="backgrnd" Color="Transparent"/>
            </Grid.Background>
            <ScrollViewer Margin="10" Height="100" Background="#FFEEEEEE">
                <Button Content="Button" Name="button1" Height="300"  Width="75" />
            </ScrollViewer>
            <Grid.Triggers>
                <EventTrigger RoutedEvent="Grid.GotFocus">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation
                            Storyboard.TargetName="backgrnd"
                            Storyboard.TargetProperty="Color"
                            To="Cyan"
                            BeginTime="0:0:0"
                            Duration="0:0:0" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Grid.Triggers>
        </Grid>
    </Grid>
</Window>
+3
source share
1 answer

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;
        // iteratively traverse the visual tree
        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>
+4

Source: https://habr.com/ru/post/1771855/


All Articles