Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to flatten a WPF TreeView

I need a control that behaves like a treeview (binds to a tree structure, expands child nodes based on IsExpanded property of bound object), yet displays data like the grid (no indenting, or toggle images).

The expand collapse will be happening automatically based on bound object.

TreeView is perfect, i just need to remove the indentation and the triangle image to make it vertically flat, like a grid column.

I suppose i could try overriding the TreeViewItem template, but that just doesn't display anything..

like image 499
Sonic Soul Avatar asked Oct 09 '22 21:10

Sonic Soul


1 Answers

Based on the TreeView style on MSDN, something like this should work:

<Style TargetType="TreeViewItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TreeViewItem">
                <StackPanel>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="ExpansionStates">
                            <VisualState x:Name="Expanded">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetProperty="(UIElement.Visibility)"
                                        Storyboard.TargetName="ItemsHost">
                                        <DiscreteObjectKeyFrame KeyTime="0"
                                            Value="{x:Static Visibility.Visible}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Collapsed" />
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ContentPresenter ContentSource="Header" />
                    <ItemsPresenter Name="ItemsHost" Visibility="Collapsed" />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
like image 113
svick Avatar answered Oct 13 '22 12:10

svick