【发布时间】:2014-08-25 11:45:03
【问题描述】:
基本上需要在wpf中使用treeview控件来实现这样的东西:(随机图片)
(来源:msdn.com)
节点和子节点具有相同的标头。
我用谷歌搜索了很多,但我对 wpf 的了解并不那么好。
这是我的父节点类:
public class Parent : PropertyChangedBase
{
public string ParentName { get; set; }
public BindableCollection<Child> Children { get; set; }
}
还有孩子:
public class Child : PropertyChangedBase
{
public string ChildName { get; set; }
}
我的 xaml 树视图:
<TreeView Grid.Row="0" Grid.Column="0" ItemsSource="{Binding Nodes}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type projectModels:Parent}" ItemsSource="{Binding Children}">
<StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="2"></CheckBox>
<TextBlock Grid.Column="1" Text="{Binding ParentName}">
</TextBlock>
</Grid>
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type projectModels:Child}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ChildName}"></TextBlock>
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
我尝试使用Grid,但显然它会创建不同的网格,因此我可以根据列宽进行中继。
我试过How to make gridview a child element of a treeview in wpf application,但他们使用ListView。这对我来说现在不是一个选项,因为树视图项选择功能与我的树视图和背后的代码紧密耦合。
有什么想法可以做到吗?谢谢。
【问题讨论】:
-
除了每个项目的列宽不同的问题之外,您的
代码是否正常工作? -
是的,它可以工作,但不是我需要的方式。我需要树视图的通用标题,它是子节点。所以我可以调整它们的大小,并且节点的大小是一样的。