ScrollViewer Focus不会更改为外部控件

时间:2014-06-16 07:37:19

标签: wpf xaml listbox

我在TreeView中使用了ListBox

        <TreeView Height="300">
            <TreeViewItem Header="Item1"/>
            <TreeViewItem Header="Item2">
                <ListBox Height="100">
                    <ListBoxItem Content="Item1"/>
                    <ListBoxItem Content="Item2"/>
                    <ListBoxItem Content="Item3"/>
                    <ListBoxItem Content="Item4"/>
                    <ListBoxItem Content="Item5"/>
                    <ListBoxItem Content="Item6"/>
                    <ListBoxItem Content="Item7"/>
                    <ListBoxItem Content="Item8"/>
                </ListBox>
            </TreeViewItem>
            <TreeViewItem Header="Item3">
                <ListBox Height="100">
                    <ListBoxItem Content="Item1"/>
                    <ListBoxItem Content="Item2"/>
                    <ListBoxItem Content="Item3"/>
                    <ListBoxItem Content="Item4"/>
                    <ListBoxItem Content="Item5"/>
                    <ListBoxItem Content="Item6"/>
                    <ListBoxItem Content="Item7"/>
                    <ListBoxItem Content="Item8"/>
                </ListBox>
            </TreeViewItem>
            <TreeViewItem Header="Item4">
                <ListBox />
            </TreeViewItem>
            <TreeViewItem Header="Item5">
                <ListBox />
            </TreeViewItem>
            <TreeViewItem Header="Item6"/>
        </TreeView>

当我在ListBox中指向鼠标点并开始滚动时,ListBox会滚动。当Scrolling结束时,我需要将焦点更改为TreeView滚动查看器,以便TreeView得到滚动。在此代码中,当鼠标位于ListBox内时,滚动对TreeView不起作用。

1 个答案:

答案 0 :(得分:0)

在ListBox滚动后我需要将焦点更改为外部控制滚动查看器

这是对您的问题的良好描述,但如果您说您希望能够将焦点从ListBox内移到ListBox之外,那么您似乎只需要学习如何使用箭头键从键盘导航ListBox

如果焦点位于其中TreeViewItem的{​​{1}},则可以按右箭头键展开该项并迭代ListBox。然后,您可以按向上和向下箭头键选择ListBoxItem中的上一个或下一个项目。

完成ListBox的查看后,您可以按左箭头键从ListBoxItem导航回TreeViewItem。然后,您可以按向上和向下箭头键再次浏览ListBox

如果这对您的问题没有帮助,请编辑您的问题并添加问题的明确说明。


更新&gt;&gt;&gt;

您可以通过处理ScrollViewer.ScrollChanged event来检测TreeViewItem何时滚动到底部:

ListBox

...

<ListBox ItemsSource="{Binding Days}" ScrollViewer.CanContentScroll="False" 
    ScrollViewer.ScrollChanged="ListBox_ScrollChanged" />

然后,您可以使用TraversalRequest classUIElement.MoveFocus method来关注下一个UI元素:

private void ListBox_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
    ScrollViewer scrollViewer = (ScrollViewer)e.OriginalSource;
    if (scrollViewer.VerticalOffset + scrollViewer.ViewportHeight == 
        scrollViewer.ExtentHeight)
    {
        // The ListBox was scrolled to the bottom
    }
}
相关问题