以编程方式关注滚动条
Programmatically Focus on a ScrollBar
在显示的代码中,
- 为什么焦点没有转移到 ScrollBar 元素?
- 为什么 UIElement.Focus (TheScrollBar.Focus) return 是假的?
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();
}
}
}
我显然在这里遗漏了一些东西。 UIElement.Focus不接受焦点吗?
简单回答:滚动条不接受焦点 :)。
在 Button_Click 方法中,添加语句 TheScrollBar.Focusable = true;
:
void Button_Click(object sender, RoutedEventArgs e)
{
TheScrollBar.Focusable = true;
TheStatus.Content = "TheScrollBar.Focus() == " + TheScrollBar.Focus().ToString();
}
...工作正常 ("Won't bust; won't rust; won't collect dust ... won't even wake the baby!")。
在显示的代码中,
- 为什么焦点没有转移到 ScrollBar 元素?
- 为什么 UIElement.Focus (TheScrollBar.Focus) return 是假的?
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();
}
}
}
我显然在这里遗漏了一些东西。 UIElement.Focus不接受焦点吗?
简单回答:滚动条不接受焦点 :)。
在 Button_Click 方法中,添加语句 TheScrollBar.Focusable = true;
:
void Button_Click(object sender, RoutedEventArgs e)
{
TheScrollBar.Focusable = true;
TheStatus.Content = "TheScrollBar.Focus() == " + TheScrollBar.Focus().ToString();
}