0

I have a TreeView with a few hundred leaves organized in a few dozen nodes... enough nodes to require scrolling to see them all.

Usually all works fine, but when the expanded nodes would not all fit within the ScrollViewer, clicking on a node to expand it also scrolls the whole TreeView back up to the top (hiding the just-expanded leaves until you scroll back down manually).

This is not the same issue as WPF TreeView with IsVirtualizing="true" jumps around when changing focus and scrolling because a) I'm not setting the vitualizing stack panel options; and b) This is .NET 4 (that link reports the issue only applied to 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>
Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553

1 Answers1

1

doesn't the treeview already have a scrollviewer defined inside it's controltemplate? try removing that scrollviewer and grid and set

<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>             
Eric J.
  • 147,927
  • 63
  • 340
  • 553
Markus Hütter
  • 7,796
  • 1
  • 36
  • 63
  • I get a compile error `The property 'HorizontalScrollBarVisibility' was not found in type 'TreeView'` However, removing the extraneous ScrollViewer did solve the problem. Editing your response to remove that error and accepting... – Eric J. Mar 24 '11 at 05:43
  • @Eric glad I could help. didn't actually have a development environment around when I wrote my answer. Upon further investigation I also found out that the scrollviewer in the standard style already is set to HorizontalScrollBarVisibility=Auto – Markus Hütter Mar 24 '11 at 23:14
  • Hi, where is this attach property defined and what does it do in the callback? – 123 456 789 0 Sep 23 '13 at 22:21