Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3308218
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:29:46+00:00 2026-05-17T21:29:46+00:00

Currently with a scrollviewer, clicking the scroll bar contained in it to pan the

  • 0

Currently with a scrollviewer, clicking the scroll bar contained in it to pan the content around it will not focus the viewer (or anything for that matter).
I wish to have it so when you scroll by dragging the scroll, or even clicking the scrollbar will focus the parent (scroll viewer).

I achieved this somewhat by attaching a ScrollChanged handler on the scrollviewer and calling sender.focus(). Unforutnally ScrollChanged is called on initialising the scrollviewer and so on loading the

I also tried attaching event setters with still no luck.

Ideally I would like it on some sort of style or something which allows me to apply it to all scrollviewers across the entire application.

Edit: Just for futher clarification, if I use this xaml and click on the scroll bar it will not turn the background blue (gotfocus event). When I click inside the scroll viewer or the button it will.

<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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:29:47+00:00Added an answer on May 17, 2026 at 9:29 pm

    I’m not 100% certain what you are looking for here but adding this code to your example will make the ScrollViewer receive focus when the ScrollBar is clicked, dragged etc. Some code behind is required for this solution though.

    <Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
        <EventSetter Event="PreviewMouseDown" Handler="scrollBar_PreviewMouseDown"/>            
    </Style>
    

    And in code behind

    void scrollBar_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        c_scrollViewer.Focus();
    }
    

    UPDATE

    You may know how to add this to a Resource Dictionary so you can access it for multiple ScrollViewers, otherwise here is how you can do it.

    Add a Resource Dictionary to your project. I called mine ScrollBarStyles.xaml.
    Add a code behind class for it, called ScrollBarStyles.xaml.cs.
    Add an x:Class attribute in the xaml file, something like

    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();
        }
    }
    

    Your Window

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ScrollBarStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a window that will be a variable size , currently it is
currently i have a textblock placed in a scroll viewer control. How do i
Currently, I am developing a product that does fairly intensive calculations using MS SQL
I have the following code currently: <DataTemplate DataType={x:Type vm:SectionViewModel}> <ScrollViewer> <ItemsControl ItemsSource={Binding ViewModels}> </ItemsControl>
I have a Canvas element, contained within a ScrollViewer, which I'm zooming using ScaleTransform.
I want to create a large view that will contain about 10-20 fields (slides,
Currently I have a service that uses a UserNamePasswordValidator to authenticate the client user.
i currently have a textblock that is embedded in a large grid. I have
I'm trying to get the maximum amount a scrollviewer can scroll in the vertical
Currently, I don't really have a good method of debugging JavaScript in Internet Explorer and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.