Using A WrapPanel in a TreeView

跟風遠走 提交于 2019-12-02 14:15:26

EDIT : i finally got this to work with a 'simpler' solution. Still it seems that we have to define the WrapPanel's Width, which make the solution less generic. (Maybe a binding of the width (but which ?) would solve this)

Here's the solution i came to : just defining, in a style, the ItemsPanel used in a TreeViewItem :

<Style TargetType="TreeViewItem">
   <Setter Property="ItemsPanel">
       <Setter.Value>
              <ItemsPanelTemplate>
                <WrapPanel  Orientation="Horizontal"      
                            Width="520"
                            HorizontalAlignment="Stretch" 
                            Margin="0" 
                            ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                            IsItemsHost="True"  
                    />
              </ItemsPanelTemplate>
       </Setter.Value>
   </Setter>
</Style>

You must set

ScrollViewer.HorizontalScrollBarVisibility="Disabled"

to your

<TreeView/>

not for

<WrapPanel/>

example:

<TreeView x:Name="fieldTreeView" Grid.Row="1" Margin="5,0,5,0" Background="Beige"         
          ItemsSource="{Binding Source={StaticResource bla}}"
          ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <TreeView.Resources>                
            <DataTemplate DataType="{x:Type Model:bla}">
                <StackPanel Orientation="Vertical">
                    <Label Content="{Binding Name}"/>
                    <TextBox Text=""/>
                </StackPanel>        
            </DataTemplate>                
        </TreeView.Resources>
        <TreeView.ItemsPanel>
            <ItemsPanelTemplate>                    
                <WrapPanel Orientation="Horizontal"/>                    
            </ItemsPanelTemplate>
        </TreeView.ItemsPanel>
    </TreeView>

This solution works for me.

Try

  1. Disable the scroll bars
  2. Widen the WrapPanel and see the visual impact, is it effected?
  3. Make a color border/background to track the actual size of the WrapPanel

These will help you trace the problem

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