1

In the code shown,

  • Why doesn't the focus shift to the ScrollBar element?
  • Why does UIElement.Focus (TheScrollBar.Focus) return False?

enter image description here

XAML:

    <Window x:Class="ScrollBarFocus.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ScrollBarFocus"
        mc:Ignorable="d"
        ResizeMode="CanMinimize"
        SizeToContent="WidthAndHeight"
        Title="MainWindow">
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button Grid.Row="0" Click="Button_Click" Content="Click me to focus on the ScrollBar"/>
        <ScrollBar x:Name="TheScrollBar" Grid.Row="1" Maximum="99" Orientation="Horizontal" SmallChange="1"/>
        <TextBox BorderBrush="Black" Grid.Row="2" IsReadOnly="True" Text="{Binding ElementName=TheScrollBar, Path=Value, StringFormat={}{0:####0}}" TextAlignment="Center"/>
        <Label x:Name="TheStatus" Grid.Row="3" Height="40" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Width="400"/>
        <x:Code>
            <![CDATA[
                void Button_Click(object sender, RoutedEventArgs e) {
                    TheStatus.Content = "TheScrollBar.Focus() == " + TheScrollBar.Focus().ToString();
                }
            ]]>
        </x:Code>
    </Grid>
</Window>

C#:

    using System.Windows;
    namespace ScrollBarFocus {
        public partial class MainWindow : Window {
            public MainWindow() {
                InitializeComponent();
            }
        }
   }

I'm obviously missing something here. Does UIElement.Focus not accept focus?

Cœur
  • 37,241
  • 25
  • 195
  • 267
We B Martians
  • 367
  • 2
  • 12

2 Answers2

1

Simple answer: Scrollbars don't accept focus :).

SledgeHammer
  • 7,338
  • 6
  • 41
  • 86
  • So ... say you want to allow a user to move, using only a keyboard, a scrollbar's thumb, just what do you do? – We B Martians Oct 13 '16 at 10:59
  • (For example, imagine you're on an airplane, and it's turbulent, and you have to get something done before you arrive, and the trackpad just won't hack it.) ...unfortunate experiences ... multiple times... – We B Martians Oct 13 '16 at 11:01
  • I guess you have to write code to handle keystrokes and then apply the value to the scrollbar ... sigh. I wonder what the reasoning was behind the decision to have scrollbars ignore focus. – We B Martians Oct 13 '16 at 11:03
  • @user1601638, are you sure you want a scroll bar? Maybe you actually want a slider control? – SledgeHammer Oct 13 '16 at 15:31
0

To the Button_Click method, add the statement TheScrollBar.Focusable = true;:

    void Button_Click(object sender, RoutedEventArgs e)
    {
        TheScrollBar.Focusable = true;
        TheStatus.Content = "TheScrollBar.Focus() == " + TheScrollBar.Focus().ToString();
    }

enter image description here ...works fine ("Won't bust; won't rust; won't collect dust ... won't even wake the baby!").

We B Martians
  • 367
  • 2
  • 12