开发者

how to flatten a WPF TreeView

开发者 https://www.devze.com 2023-04-07 14:48 出处:网络
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 im

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..


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>


You need a TreeListView (it combines the TreeView and ListView at TreeViewItem template level beautifully)

http://msdn.microsoft.com/en-us/library/ms771523.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号