2011-03-23 121 views
0

我有一個TreeView,有數百個樹葉,組織在幾十個節點中......足夠的節點需要滾動才能看到它們。WPF TreeView跳轉

通常所有的工作都很好,但是當展開的節點不會全部適合ScrollViewer時,單擊一個節點來展開它也會將整個TreeView滾動回頂部(隱藏剛剛展開的葉子,直到您向後滾動手動下降)。

這不是WPF TreeView with IsVirtualizing="true" jumps around when changing focus and scrolling問題,因爲a)我沒有設置vitualizing堆棧面板選項;和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

不樹形視圖已經有它裏面的控件模板中定義的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>    
+0

我得到一個編譯錯誤'屬性「Horizo​​ntalScrollBarVisibility」型「TreeView''沒有被發現。然而,去除外部ScrollViewer中沒有解決的問題。編輯你的回覆,以消除該錯誤,並接受... – 2011-03-24 05:43:14

+0

@Eric很高興我可以幫助。在我寫回答時,實際上並沒有一個開發環境。經過進一步調查,我還發現標準樣式的scrollviewer已經設置爲Horizo​​ntalScrollBarVisibility = Auto – 2011-03-24 23:14:53

+0

嗨,這個attach屬性在哪裏定義,它在回調中做了什麼? – 2013-09-23 22:21:49