WPF: TreeView or TreeListView Headers Horizontal Scrolling Issue

╄→尐↘猪︶ㄣ 提交于 2019-12-22 08:26:09

问题


I downloaded TreeListView from here. It did not showed Horizontal or Vertical Scroll bar when data is clipped. Like this

So I changed its Style to

<Style TargetType="{x:Type l:TreeListView}">
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility"
            Value="Auto" />
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility"
            Value="Auto" />
    <Setter Property="VerticalContentAlignment"
            Value="Center" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type l:TreeListView}">
                <Border BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <DockPanel>
                        <GridViewHeaderRowPresenter Columns="{StaticResource gvcc}"
                                                    DockPanel.Dock="Top" />
                        <ScrollViewer x:Name="_tv_scrollviewer_"
                                      Background="{TemplateBinding Background}"
                                      CanContentScroll="False"
                                      Focusable="True"
                                      HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                                      Padding="{TemplateBinding Padding}"
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                      VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}">
                            <ItemsPresenter />
                        </ScrollViewer>
                    </DockPanel>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="VirtualizingStackPanel.IsVirtualizing"
                             Value="true">
                        <Setter Property="CanContentScroll"
                                TargetName="_tv_scrollviewer_"
                                Value="true" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Vertical Scrollbar is fine. But the problem is Horizontal Scollbar. When data gets clipped horizontally, and scrollbar is moved on right, the data moves right but headers stay where they are. Like this.

How to overcome this problem that when treeitem is scrolled horizontally, headers move with it. I am not allowed to put headers in scrollviewer because they need to be visible when data is scrolled vertically.


回答1:


I had the same issue with this kind of tree and now is solved by sincronizing scrollbars. I added a scroll bar to the GridViewHeaderRowPresenter that is hidded horizontaly and disabled vertically, and I used a code from codeproject that already implements sincronization of scrollbars.

<DockPanel>
        <ScrollViewer DockPanel.Dock="Top" HorizontalScrollBarVisibility="Hidden"
                    VerticalScrollBarVisibility ="Disabled"
                    controls:ScrollSynchronizer.ScrollGroup="Group1">
          <GridViewHeaderRowPresenter Columns="{StaticResource gvcc}"/>
         </ScrollViewer>
         <ScrollViewer  
                     HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                     VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
                     controls:ScrollSynchronizer.ScrollGroup="Group1">
                  <ItemsPresenter />
          </ScrollViewer>
</DockPanel>

Link to the library http://www.codeproject.com/Tips/564665/Synchronize-Two-Scroll-Viewers-in-WPF and the original article for the lib http://www.codeproject.com/Articles/39244/Scroll-Synchronization.




回答2:


If you want the complete treeview (including the header) to scroll horizontally, additionanly add a scrolviewer like this:

<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" >
    <Treeview/>
</ScrollViewer>



回答3:


I believe setting HorizontalScrollBarVisibility="Disabled" should force the contents of the scrollviewer inside. With the scrollbar disabled, it should keep it from messing up the render. This has actually been a problem for a couple of versions. I am unsure if it has been fixed in the latest WPF.



来源:https://stackoverflow.com/questions/16162067/wpf-treeview-or-treelistview-headers-horizontal-scrolling-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!