目前,使用滚动视图器中的滚动条来平移其周围的内容时,将不会聚焦于视图器(或其他任何东西)。我希望当您通过拖动滚动条或单击滚动条滚动时,可以聚焦于父级(滚动视图器)。
我通过在滚动视图器上附加ScrollChanged处理程序并调用sender.focus()来实现了这一点。但不幸的是,在初始化滚动视图器时会调用ScrollChanged,因此在加载时也会调用。
我还尝试过附加事件设置器,但仍然没有运气。
理想情况下,我希望它能够应用于整个应用程序中的所有滚动视图器,例如某种样式或其他方式。
编辑:为了进一步澄清,如果我使用此xaml并单击滚动条,则不会使背景变蓝(gotfocus事件)。当我单击滚动视图器内部或按钮时,它会。
<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>