我正在从以下问题的答案中进行工作:Display treeviewitem as grid rows in wpf,而我正努力使要选择的树中的项目得到解决。您如何使用这种类型的代码呢?谢谢
我尝试制作TextBlocks和TextBoxes,但是没有运气。
<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#代码
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;
答案 0 :(得分:1)
您的元素是不可选择的,因为它们在TextBlocks
中是简单的DataTemplate
,因此没有任何可选择的行为。为了使它们可选,它们将必须为TreeViewItem
类型,因为WPF会处理这些选择。
您不应像这样做那样手动模板TreeView
,而应使用HierarchicalDataTemplate
来让TreeView
像这样建立其TreeViewItems
的集合:>
<TreeView ItemsSource="{Binding ManufacturerList}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type wpfapp1:Company}" ItemsSource="{Binding Models}">
<Grid>
<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>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type wpfapp1:Model}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"></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>
</TreeView.Resources>
</TreeView>
关于您的对齐需求,我对Width="80"
和Width="100"
进行了硬编码,因为您已经有硬编码的值,但是我认为这不是可行的方法(存在更干净的解决方案)。