WPF TreeView跳转

时间:2011-03-23 07:37:08

标签: wpf .net-4.0 treeview

我有一个TreeView,在几十个节点中组织了几百个叶子......足够的节点需要滚动才能看到它们。

通常一切正常,但是当展开的节点不能全部适合ScrollViewer时,点击一个节点展开它也会将整个TreeView向上滚动到顶部(隐藏刚刚展开的叶子直到你向下滚动手动地)。

这与WPF TreeView with IsVirtualizing="true" jumps around when changing focus and scrolling不同,因为a)我没有设置vitualizing stack panel选项;和b)这是.NET 4(该链接报告仅适用于3.5的问题)。

<DataTemplate x:Key="QuestionTemplate">
    <Border BorderBrush="AliceBlue" BorderThickness="1">
        <HeaderedContentControl HeaderTemplate="{DynamicResource QuestionHeaderTemplate}" 
                  Header="{Binding NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" IsTabStop="False" HorizontalAlignment="Left" 
                  IsEnabled="True" />                        
    </Border>
</DataTemplate>
    <HierarchicalDataTemplate x:Key="GroupTemplate" ItemsSource="{Binding MyData, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}" 
                                          ItemTemplate="{StaticResource MyTemplate}">                
        <Border x:Name="GroupTemplateBorder" BorderBrush="AliceBlue" BorderThickness="1" CornerRadius="10">
        <HeaderedContentControl x:Name="GroupTemplateExpander" MinHeight="22" HeaderTemplate="{DynamicResource GroupHeaderTemplate}" Header="{Binding}" IsTabStop="False" 
            HorizontalAlignment="Left" VerticalContentAlignment="Center" IsEnabled="True" />                   
        </Border>
    </HierarchicalDataTemplate>

    <ScrollViewer HorizontalScrollBarVisibility="Auto">
    <Grid Name="gridPicker" Background="White" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
    <TreeView Name="treeView" MinHeight="20" ItemTemplate="{StaticResource GroupTemplate}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
        <TreeView.ItemContainerStyle>
            <Style TargetType="TreeViewItem">
                <Setter Property="helper:TreeViewNoSelectBehavior.IsTransparent" Value="True" />
            </Style>
        </TreeView.ItemContainerStyle>
    </TreeView>                
    </Grid>
    </ScrollViewer>

1 个答案:

答案 0 :(得分:1)

树视图中是否已经在其controltemplate中定义了一个scrollviewer?尝试删除该scrollviewer和网格并设置

<TreeView Name="treeView" Background="White" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" MinHeight="20" ItemTemplate="{StaticResource GroupTemplate}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
    <TreeView.ItemContainerStyle>
        <Style TargetType="TreeViewItem">
            <Setter Property="helper:TreeViewNoSelectBehavior.IsTransparent" Value="True" />
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>