以编程方式关注ScrollBar

时间:2016-10-12 21:40:05

标签: c# wpf focus scrollbar uielement

在显示的代码中

  • 为什么焦点不会转移到ScrollBar元素?
  • 为什么UIElement.Focus(TheScrollBar.Focus)返回False?

enter image description here

XAML:

import qualified Data.Vector.Unboxed.Mutable as M
import Data.Vector.Unboxed
import Control.Monad.ST
import Control.Monad

createCustomeInitializedArray :: ST s (Vector Int)
createCustomeInitializedArray = do
     arr <- M.new 10
     -- ... 
     iarr <- freeze arr
     return iarr

C#:

    <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>

我显然在这里遗漏了一些东西。 UIElement.Focus不接受焦点吗?

2 个答案:

答案 0 :(得分:1)

简单回答:滚动条不接受焦点:)。

答案 1 :(得分:0)

对Button_Click方法添加语句TheScrollBar.Focusable = true;

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

enter image description here ......工作得很好(&#34;赢了,不会生锈;不会收集灰尘......赢了甚至叫醒了宝宝!&#34;)。