WPF实现带有Checkbox选择框的TreeView树控件

本文展示了如何在WPF中创建一个带有Checkbox的TreeView控件,详细讲解了XAML和C#代码实现过程,包括数据绑定、图像转换和目录结构的递归加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WPF实现带有Checkbox选择框的TreeView树控件


代码片段:

Window1.xaml文件

<Window x:Class="CheckBoxTreeView.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:l="clr-namespace:CheckBoxTreeView"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <ResourceDictionary>
            <l:Directory2ImageSource x:Key="Directory2ImageSource"/>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <TreeView Name="tvDirectories" Grid.Column="0" Margin="0,0,5,0">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate DataType="{x:Type l:DirectoryTree}" ItemsSource="{Binding Directories}">
                    <StackPanel Orientation="Horizontal">
                        <CheckBox IsChecked="{Binding Path=IsChecked}" IsTabStop="False" Focusable="False" HorizontalAlignment="Center"/>
                        <Image Source="{Binding Path=Info.FullName, Converter={StaticResource Directory2ImageSource}}" HorizontalAlignment="Center"/>
                        <TextBlock Text="{Binding Path=Info.Name}" HorizontalAlignment="Center"/>
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</Window>

Window1.xaml.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CheckBoxTreeView
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            this.tvDirectories.ItemsSource = DirectoryTree.InitRoot();
        }
    }

    class Directory2ImageSource : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            String path = value as String;
            if (System.IO.Directory.Exists(path))
                return ExtractIconAndType.GetIcon(path, false, true);
            else if (System.IO.File.Exists(path))
                return ExtractIconAndType.GetIcon(path, false, true);
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}

ExtractIcon.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using S

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值