WPF treeview例子
时间: 2023-12-31 12:22:24 浏览: 162
以下是一个基于WPF的treeview树形多列用户控件的例子,你可以参考它来实现你自己的需求:
```xml
<Window x:Class="WpfTreeViewExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TreeView ItemsSource="{Binding}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" Grid.Column="0"/>
<TextBlock Text="{Binding Description}" Grid.Column="1"/>
</Grid>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Window>
```
```csharp
public class TreeViewItem
{
public string Name { get; set; }
public string Description { get; set; }
public ObservableCollection<TreeViewItem> Children { get; set; } = new ObservableCollection<TreeViewItem>();
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var root = new TreeViewItem { Name = "Root", Description = "This is the root item" };
root.Children.Add(new TreeViewItem { Name = "Child 1", Description = "This is the first child item" });
root.Children.Add(new TreeViewItem { Name = "Child 2", Description = "This is the second child item" });
DataContext = new ObservableCollection<TreeViewItem> { root };
}
}
```
阅读全文