如何展开一个WPF TreeView

5
我需要一个行为类似于树形视图的控件(绑定到树形结构,根据绑定对象的IsExpanded属性展开子节点),但像网格一样显示数据(无缩进或切换图像)。
展开和折叠将根据绑定对象自动发生。
TreeView很完美,我只需要删除缩进和三角形图像,使其垂直平坦,就像网格列一样。
我想我可以尝试覆盖TreeViewItem模板,但那样什么也不会显示。
2个回答

2

根据MSDN上TreeView样式,下面这样的代码应该可以工作:

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

2

谢谢,这正是我在寻找的!你提供的链接是正确的,但 MSDN 没有链接到实际的代码!代码可以在这里找到(http://archive.msdn.microsoft.com/wpfsamples)。 - Lee Campbell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接