问题描述
是否有一种方法可以自动在WPF中从树形视图扩展所有节点?我进行了搜索,甚至在treeview属性中都没有找到扩展功能.
Is there a way to automatically expand all nodes from a treeview in WPF? I searched and didn't even find an expand function in the treeview property.
谢谢
推荐答案
您可以设置ItemContainerStyle并使用IsExpanded属性.
You can set ItemContainerStyle and use IsExpanded property.
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<TreeView>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeViewItem Header="Header 1">
<TreeViewItem Header="Sub Item 1"/>
</TreeViewItem>
<TreeViewItem Header="Header 2">
<TreeViewItem Header="Sub Item 2"/>
</TreeViewItem>
</TreeView>
</Grid>
</Page>
如果需要通过代码执行此操作,则可以为树视图项目编写viewmodel,并将IsExpanded属性绑定到模型中的相应属性.有关更多示例,请参见Josh Smith在CodeProject上的出色文章:使用ViewModel简化WPF TreeView模式
If you need to do this from code, you can write viewmodel for your tree view items, and bind IsExpanded property to corresponding one from model. For more examples refer to great article from Josh Smith on CodeProject: Simplifying the WPF TreeView by Using the ViewModel Pattern
这篇关于WPF中的AutoExpand树形视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!