如何以编程方式填充 WPF 多列 TreeView?
How to populate a WPF multi column TreeView programmatically?
我是 WPF
的新手,我花了最后几个小时尝试添加一个 TreeView
,其中的项目包含多列。我在互联网上寻找类似的解决方案,我认为我的问题是完全一样的,正如这个答案中所解释的:How to bind WPF TreeView to a List programmatically? 但是,我似乎无法解决它,所以我想问寻求帮助。
这是我在 XAML
:
中的代码
<TreeView Name="treeView1">
<TreeView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding carModel}" />
<TextBlock Text="{Binding carHorsePower}" />
</Grid>
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
这是C#
背后的代码:
CarDealer carDealer = GetDealerCars();
foreach(var groupedCar in carDealer.cars.GroupBy(x => x.carBrand).ToList())
{
TreeViewItem item = new TreeViewItem();
item.Header = groupedCar.Key; // This works fine, it shows the brands in the tree view
item.ItemsSource = groupedCar; // This is not shown properly, because I am not sure how to bind the carModel and the carHorsePower properties from the list
treeView1.Items.Add(item);
}
CarDealer
class 看起来像这样:
public class CarDealer
{
public string dealerAddress { get; set; }
public List<Cars> cars { get; set;}
}
public class Cars
{
public string carBrand { get; set; }
public string carModel { get; set; }
public string carHorsePower { get; set; }
}
目前显示的是树视图,带有 headers,但我不确定如何添加多列,而不是列,现在它只显示 MyTestWpfApplication.Cars
,如上面的 link 所示。我想我必须创建文本块并将数据从我的 List<Cars>
绑定到它们。
编辑:我想要实现的是这样的:
修改你的xaml:
<Window.Resources>
<DataTemplate x:Key="subItems">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding carModel}" />
<TextBlock Text="{Binding carHorsePower}" Margin="5, 0, 5, 0" Background="Bisque" Grid.Column="1"/>
</Grid>
</DataTemplate>
</Window.Resources>
<StackPanel>
<TreeView Name="treeView1" Loaded="treeView1_Loaded">
</TreeView>
</StackPanel>
然后修改你的c#代码:
CarDealer carDealer = GetDealerCars();
foreach (var groupedCar in carDealer.cars.GroupBy(x => x.carBrand).ToList())
{
TreeViewItem item = new TreeViewItem();
item.Header = groupedCar.Key;
var src = new List<Cars>(groupedCar);
item.ItemTemplate = (DataTemplate)FindResource("subItems");
item.ItemsSource = src;
treeView1.Items.Add(item);
}
但话又说回来,如果您投入更多时间在 MVVM 中使用模板等转换您的项目。这会更好。
我不知道怎么做,因为我没有更多时间,所以我决定使用 ListView
和 GridView
,这实际上更容易实现。
对于其他尝试做同样事情的人来说,这是一个很好的入门教程:http://www.wpf-tutorial.com/listview-control/listview-grouping/
我是 WPF
的新手,我花了最后几个小时尝试添加一个 TreeView
,其中的项目包含多列。我在互联网上寻找类似的解决方案,我认为我的问题是完全一样的,正如这个答案中所解释的:How to bind WPF TreeView to a List programmatically? 但是,我似乎无法解决它,所以我想问寻求帮助。
这是我在 XAML
:
<TreeView Name="treeView1">
<TreeView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding carModel}" />
<TextBlock Text="{Binding carHorsePower}" />
</Grid>
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
这是C#
背后的代码:
CarDealer carDealer = GetDealerCars();
foreach(var groupedCar in carDealer.cars.GroupBy(x => x.carBrand).ToList())
{
TreeViewItem item = new TreeViewItem();
item.Header = groupedCar.Key; // This works fine, it shows the brands in the tree view
item.ItemsSource = groupedCar; // This is not shown properly, because I am not sure how to bind the carModel and the carHorsePower properties from the list
treeView1.Items.Add(item);
}
CarDealer
class 看起来像这样:
public class CarDealer
{
public string dealerAddress { get; set; }
public List<Cars> cars { get; set;}
}
public class Cars
{
public string carBrand { get; set; }
public string carModel { get; set; }
public string carHorsePower { get; set; }
}
目前显示的是树视图,带有 headers,但我不确定如何添加多列,而不是列,现在它只显示 MyTestWpfApplication.Cars
,如上面的 link 所示。我想我必须创建文本块并将数据从我的 List<Cars>
绑定到它们。
编辑:我想要实现的是这样的:
修改你的xaml:
<Window.Resources>
<DataTemplate x:Key="subItems">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding carModel}" />
<TextBlock Text="{Binding carHorsePower}" Margin="5, 0, 5, 0" Background="Bisque" Grid.Column="1"/>
</Grid>
</DataTemplate>
</Window.Resources>
<StackPanel>
<TreeView Name="treeView1" Loaded="treeView1_Loaded">
</TreeView>
</StackPanel>
然后修改你的c#代码:
CarDealer carDealer = GetDealerCars();
foreach (var groupedCar in carDealer.cars.GroupBy(x => x.carBrand).ToList())
{
TreeViewItem item = new TreeViewItem();
item.Header = groupedCar.Key;
var src = new List<Cars>(groupedCar);
item.ItemTemplate = (DataTemplate)FindResource("subItems");
item.ItemsSource = src;
treeView1.Items.Add(item);
}
但话又说回来,如果您投入更多时间在 MVVM 中使用模板等转换您的项目。这会更好。
我不知道怎么做,因为我没有更多时间,所以我决定使用 ListView
和 GridView
,这实际上更容易实现。
对于其他尝试做同样事情的人来说,这是一个很好的入门教程:http://www.wpf-tutorial.com/listview-control/listview-grouping/