用WPF生成两个树形控件,第一个树形控件根据指定文件夹路径生成,第二个树形控件数据源是根据第一个树形控件的选择项改变,两个树形控件采用垂直布局
时间: 2024-05-09 09:19:27 浏览: 140
这里提供一份简单的代码示例:
MainWindow.xaml:
```xml
<Window x:Class="WpfApp1.MainWindow"
...
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TreeView x:Name="TreeView1" Grid.Column="0"
ItemsSource="{Binding Drives}"
SelectedItemChanged="TreeView1_SelectedItemChanged">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:DriveInfoWrapper}" ItemsSource="{Binding Directories}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<TreeView x:Name="TreeView2" Grid.Column="1"
ItemsSource="{Binding SelectedDirectory.Directories}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:DirectoryInfoWrapper}" ItemsSource="{Binding Directories}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Window>
```
MainWindow.xaml.cs:
```csharp
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public ObservableCollection<DriveInfoWrapper> Drives { get; set; }
public DirectoryInfoWrapper SelectedDirectory { get; set; }
public MainWindow()
{
InitializeComponent();
// 初始化第一个树形控件的数据源
Drives = new ObservableCollection<DriveInfoWrapper>();
foreach (var drive in DriveInfo.GetDrives())
{
Drives.Add(new DriveInfoWrapper(drive));
}
DataContext = this;
}
private void TreeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
// 根据第一个树形控件的选择项更新第二个树形控件的数据源
SelectedDirectory = (e.NewValue as DirectoryInfoWrapper);
TreeView2.ItemsSource = SelectedDirectory.Directories;
}
}
public class DriveInfoWrapper
{
public DriveInfo DriveInfo { get; set; }
public string Name => DriveInfo.Name;
public ObservableCollection<DirectoryInfoWrapper> Directories { get; set; }
public DriveInfoWrapper(DriveInfo driveInfo)
{
DriveInfo = driveInfo;
Directories = new ObservableCollection<DirectoryInfoWrapper>();
foreach (var directory in DriveInfo.RootDirectory.EnumerateDirectories())
{
Directories.Add(new DirectoryInfoWrapper(directory));
}
}
}
public class DirectoryInfoWrapper
{
public DirectoryInfo DirectoryInfo { get; set; }
public string Name => DirectoryInfo.Name;
public ObservableCollection<DirectoryInfoWrapper> Directories { get; set; }
public DirectoryInfoWrapper(DirectoryInfo directoryInfo)
{
DirectoryInfo = directoryInfo;
Directories = new ObservableCollection<DirectoryInfoWrapper>();
foreach (var directory in DirectoryInfo.EnumerateDirectories())
{
Directories.Add(new DirectoryInfoWrapper(directory));
}
}
}
}
```
其中,DriveInfoWrapper 和 DirectoryInfoWrapper 是对 DriveInfo 和 DirectoryInfo 的包装类,用于绑定到树形控件上。在 DriveInfoWrapper 中,每个驱动器根目录下的所有子目录都会被包装成一个 DirectoryInfoWrapper 对象,存放在 Directories 属性中;在 DirectoryInfoWrapper 中,每个子目录的子目录也会被包装成一个 DirectoryInfoWrapper 对象,存放在 Directories 属性中。
在 MainWindow 的构造函数中,初始化了第一个树形控件的数据源 Drives,这里只是简单地获取了所有驱动器的信息,并将每个驱动器根目录下的所有子目录包装成 DirectoryInfoWrapper 对象。在 TreeView1_SelectedItemChanged 事件处理程序中,根据第一个树形控件的选择项更新第二个树形控件的数据源,即将 SelectedDirectory.Directories 绑定到 TreeView2 的 ItemsSource 属性上。
最后,在 XAML 文件中定义了两个树形控件,分别绑定到 Drives 和 SelectedDirectory.Directories。其中,第一个树形控件的 ItemTemplate 使用了 DriveInfoWrapper 的数据类型,第二个树形控件的 ItemTemplate 使用了 DirectoryInfoWrapper 的数据类型。
阅读全文