Wpf 如何使scrollviewer中的滚动条在单击时聚焦scrollviewer?
当前使用scrollviewer时,单击其中包含的滚动条以平移其周围的内容将不会聚焦查看器(或任何相关内容)。 我希望当您拖动滚动条滚动时,或者甚至单击滚动条将焦点对准父对象(滚动查看器) 我通过在scrollviewer上附加ScrollChanged处理程序并调用sender.focus()实现了这一点。不幸的是,在初始化scrollviewer等加载 我还试着在仍然没有运气的情况下连接事件设定者 理想情况下,我希望它以某种形式出现,或者允许我将其应用于整个应用程序中的所有ScrollViewer 编辑:只是为了进一步澄清,如果我使用这个xaml并单击滚动条,它不会将背景变成蓝色(gotfocus事件)。当我在滚动查看器或按钮中单击时,它将显示。Wpf 如何使scrollviewer中的滚动条在单击时聚焦scrollviewer?,wpf,Wpf,当前使用scrollviewer时,单击其中包含的滚动条以平移其周围的内容将不会聚焦查看器(或任何相关内容)。 我希望当您拖动滚动条滚动时,或者甚至单击滚动条将焦点对准父对象(滚动查看器) 我通过在scrollviewer上附加ScrollChanged处理程序并调用sender.focus()实现了这一点。不幸的是,在初始化scrollviewer等加载 我还试着在仍然没有运气的情况下连接事件设定者 理想情况下,我希望它以某种形式出现,或者允许我将其应用于整个应用程序中的所有ScrollVie
<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>
我不能100%确定您在这里寻找的是什么,但将此代码添加到示例中会使ScrollViewer在单击、拖动滚动条等时获得焦点。不过,此解决方案需要一些代码
<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
<EventSetter Event="PreviewMouseDown" Handler="scrollBar_PreviewMouseDown"/>
</Style>
更新
您可能知道如何将其添加到资源字典中,以便可以为多个ScrollViewer访问它,否则,下面是您的方法
将资源字典添加到项目中。我给我的ScrollBarStyles.xaml打了电话。为它添加一个名为ScrollBarStyles.xaml.cs的代码隐藏类。
在xaml文件中添加一个x:Class属性,类似
x:Class="YourNameSpace.ScrollBarStyles"
ScrollBarStyles.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="FocusScrollViewer.ScrollBarStyles">
<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
<EventSetter Event="PreviewMouseDown" Handler="scrollBar_PreviewMouseDown"/>
</Style>
</ResourceDictionary>
ScrollBarStyles.xaml.cs
public partial class ScrollBarStyles
{
public T GetVisualParent<T>(object childObject) where T : Visual
{
DependencyObject child = childObject as DependencyObject;
// iteratively traverse the visual tree
while ((child != null) && !(child is T))
{
child = VisualTreeHelper.GetParent(child);
}
return child as T;
}
void scrollBar_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
ScrollBar scrollBar = sender as ScrollBar;
ScrollViewer scrollViewer = GetVisualParent<ScrollViewer>(scrollBar);
scrollViewer.Focus();
}
}
public分部类滚动条样式
{
公共T GetVisualParent(对象子对象),其中T:Visual
{
DependencyObject子对象=作为DependencyObject的子对象;
//迭代遍历可视化树
while((child!=null)&&&!(child是T))
{
child=visualtreeheloper.GetParent(child);
}
返回子对象作为T;
}
无效滚动条\u预览鼠标向下(对象发送器,鼠标按钮Ventargs e)
{
ScrollBar ScrollBar=发送方为ScrollBar;
ScrollViewer ScrollViewer=GetVisualParent(滚动条);
scrollViewer.Focus();
}
}
你的窗户
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ScrollBarStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
这是一个非常好的答案。非常感谢。虽然我之前已经为特定的滚动条编写了代码,以便像您一样进行聚焦,但我不知道如何以一种风格进行。事实上,我没有意识到资源字典可能有代码落后。非常有价值的信息。谢谢
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ScrollBarStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>