基本上需要在wpf :(随机图片)
中使用treeview控件来实现这样的功能http://blogs.msdn.com/blogfiles/delay/SimpleTreeGridUX.png
节点和子节点具有相同的标头。
我google了很多,但我对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
。它现在不适合我,因为treeviewitem选择功能与我的树视图和后面的代码紧密结合。
有什么想法可以做到吗?感谢。
答案 0 :(得分:13)
试试这个 的 XAML 强>
<TreeView x:Name="treeviewList" ItemsSource="{Binding ManufacturerList}">
<TreeView.ItemTemplate>
<DataTemplate>
<TreeViewItem ItemsSource="{Binding Models}">
<TreeViewItem.Header>
<Grid Width="350">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="Auto" MinWidth="50"></ColumnDefinition>
<ColumnDefinition ></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Task}" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="0"/>
<TextBlock Text="{Binding durationTotal}" Margin="10,0,10,0" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="1"/>
<TextBlock Text="{Binding HeadNote}" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="2"/>
</Grid>
</TreeViewItem.Header>
<TreeViewItem.ItemTemplate>
<DataTemplate>
<Grid Margin="-20,0,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="Auto" MinWidth="50"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding SubTask}" Margin="10,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="0"/>
<TextBlock Text="{Binding Duration}" Margin="10,0,10,0" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="1"/>
<TextBlock Text="{Binding Notes}" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="2"/>
</Grid>
</DataTemplate>
</TreeViewItem.ItemTemplate>
</TreeViewItem>
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
c#code
public class Company
{
public string Task { get; set; }
public string durationTotal { get; set; }
public string HeadNote { get; set; }
public List<Model> Models { get; set; }
}
public class Model
{
public string SubTask { get; set; }
public string Duration { get; set; }
public string Notes { get; set; }
}
List<Company> ManufacturerList = new List<Company>();
ManufacturerList.Add(new Company()
{
Task = "Coding",
durationTotal = "4",
HeadNote = "Coding Task",
Models = new List<Model>()
{new Model(){SubTask = "Write", Duration = "2", Notes ="It pays the bills" },
new Model(){SubTask = "Compile", Duration = "1", Notes ="c# or go home" },
new Model(){SubTask = "Test", Duration = "1", Notes ="works on my m/c" },}
});
ManufacturerList.Add(new Company()
{
Task = "Communicate",
durationTotal = "2",
HeadNote = "Communicate Task",
Models = new List<Model>()
{new Model(){SubTask = "Email", Duration = "0.5", Notes ="so much junk mail" },
new Model(){SubTask = "Blogs", Duration = "0.25", Notes ="blogs.msdn.com/delay" },
new Model(){SubTask = "Twitter", Duration = "0.25", Notes ="RT:nothing to report" },}
});
treeviewList.ItemsSource = ManufacturerList;
<强>结果强>
答案 1 :(得分:4)
如果您的代码唯一的问题是每个树视图项呈现不同的网格列宽度,您可以尝试“共享大小范围”功能将它们全部对齐。在TreeView控件中,将Grid.IsSharedSizeScope
设置为true: -
然后将SharedSizeGroup
添加到所有树视图项目中应具有相同宽度的ColumnDefinition
(无论如何,您的第一列定义具有固定宽度,因此不需要它): -
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="Auto" SharedSizeGroup="A" />
<ColumnDefinition SharedSizeGroup="B" />
</Grid.ColumnDefinitions>
值只是用于“命名”列的字符串,可以是您喜欢的任何内容。