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 7895417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:38:19+00:00 2026-06-03T07:38:19+00:00

I have a Textblock within a scrollviewer around it. My application is completely controlled

  • 0

I have a Textblock within a scrollviewer around it. My application is completely controlled by remote so in this context navigation is performed with key up, down, left and right.

I can navigate into the Textblock but then I get trapped there. I’ve tried placing KeyboardNavigation.DirectionalNavigation=”Continue” in everycontrol I can but no joy.

I then thought of making a customcontrol that extends the scrollbar or the scrollviewer.
If extending the scrollbar I can override keydown as per the following.

protected override void OnPreviewKeyDown(KeyEventArgs e)
    {
        if (this.Orientation == Orientation.Vertical)
        {
            if (e.Key == Key.Up)
            {
                if (this.Value == this.Minimum)
                {
                    this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up));
                    e.Handled = true;
                }
            }
            if (e.Key == Key.Down)
            {
                if (this.Value == this.Maximum)
                {
                    this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
                    e.Handled = true;
                }
            }
            if (e.Key == Key.Left)
            {
                this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Left));
                e.Handled = true;
            }
            if (e.Key == Key.Right)
            {
                this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Right));
                e.Handled = true;
            }
        }

        if (this.Orientation == Orientation.Horizontal)
        {
            if (e.Key == Key.Left)
            {
                if (this.Value == this.Minimum)
                {
                    this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Left));
                    e.Handled = true;
                }
            }
            if (e.Key == Key.Right)
            {
                if (this.Value == this.Maximum)
                {
                    this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Right));
                    e.Handled = true;
                }
            }
            if (e.Key == Key.Up)
            {
                this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up));
                e.Handled = true;
            }
            if (e.Key == Key.Down)
            {
                this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
                e.Handled = true;
            }
        }
        base.OnPreviewKeyDown(e);
    }
}

The problem is I’m not sure how to change the ScrollViewer scrollbar to use a custom one or even if the code above will even fire when a key is pressed. I presume the text block and the scrollviewer will be the controls that see the event.

Is there a way to do something similar to the code above but in the Scrollviewers code behind?

  • 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-06-03T07:38:20+00:00Added an answer on June 3, 2026 at 7:38 am

    I solved this in the end by creating a custom control. I work out if the scrollviewer can scroll in the direction the key has been pressed. If yes the event passes through to the underlying scrollviewer. If not the event is marked as handled and the focus is moved in the direction of the key press.

    public class KNScrollViewer : ScrollViewer
    {
        static KNScrollViewer()
        {
    
        }
    
        private bool canScrollUp
        {
            get
            {
                return this.ScrollableHeight > 0 && this.VerticalOffset > 0;
            }
        }
    
        private bool canScrollDown
        {
            get
            {
                return this.ScrollableHeight > 0 &&
                  this.VerticalOffset + this.ViewportHeight < this.ExtentHeight;
            }
        }
    
        private bool canScrollLeft
        {
            get
            {
                return this.ScrollableWidth > 0 && this.HorizontalOffset > 0;
            }
        }
    
        private bool canScrollRight
        {
            get
            {
                return this.ScrollableWidth > 0 &&
                this.HorizontalOffset + this.ViewportWidth < this.ExtentWidth;
            }
        }
    
        public bool CanScroll
        {
            get
            {
                if (canScrollUp || canScrollDown || canScrollLeft || canScrollRight)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    
        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.Up)
            {
                if (!canScrollUp)
                {
                    this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up));
                    e.Handled = true;
                }
            }
            if (e.Key == Key.Down)
            {
                if (!canScrollDown)
                {
                    this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
                    e.Handled = true;
                }
            }
            if (e.Key == Key.Left)
            {
                if (!canScrollLeft)
                {
                    this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Left));
                    e.Handled = true;
                }
            }
            if (e.Key == Key.Right)
            {
                if (!canScrollRight)
                {
                    this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Right));
                    e.Handled = true;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following DataTemplate : <DataTemplate x:Key=ColoringLabels> <TextBlock Padding=0 Margin=0 Name=Username Text={Binding Username}
I have an ItemsControl with the following ItemTemplate: <DataTemplate x:Key=myItemTemplate> <TextBlock Height=??? Text={Binding Path=Description}
Basically I have a WPF application that will display announcements to the user within
I have a style for a textblock that is set inside my app.xaml this
I have a textblock within a GridView that is bound to a property that
I have a textblock that is bound to an object. This object I have
Lets say I have this bit of code: <Window> <Window.Resources> <Color x:Key=MyColor A=255 R=152
I have a Listbox within my WP7 application, and inside the DataTemplate I have
I have a TextBlock within a DockPanel within a Border . The TextBlock is
In my WP7 app, I'm using this guys method to find a TextBlock within

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.