带有CheckBoxes的TreeView动态地WPF。如何知道所选元素的名称 [英] TreeView with CheckBoxes dynamically WPF. How to know the name of the elements selected

查看:87
本文介绍了带有CheckBoxes的TreeView动态地WPF。如何知道所选元素的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,可在TreeView中显示文件和文件夹的名称,并在每个元素中放置一个复选框。我不知道该怎么办,是如何知道TreeView中被复选框选中的元素。

I have a code that displays the files and folders names in a TreeView and put a checkbox from each element. What I don't know how to do is how to know the elements in the TreeView that are selected with the checkboxes.

XAML:

<TreeView Name="treeView" Grid.Row="10" Grid.ColumnSpan="3">
        <TreeView.Resources>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <CheckBox Focusable="False" IsChecked="False" VerticalAlignment="Center"/>
                                <TextBlock Text="{Binding}" Margin="5,0" />
                            </StackPanel>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TreeView.Resources>
    </TreeView>

程序:

    DirectoryInfo di = new DirectoryInfo(folder);
    treeView.Items.Add(getTree(di));

    public TreeViewItem getTree(DirectoryInfo di)
    {
        TreeViewItem item = new TreeViewItem();
        item.Header = di.Name;
        item.FontWeight = FontWeights.Normal;
        foreach (DirectoryInfo s in di.GetDirectories())
        {
            item.Items.Add(getTree(s)); 
        }
        foreach (FileInfo fi in di.GetFiles())
        {
            item.Items.Add(fi.Name);
        }
        return item;
    }


推荐答案

创建自己的 TreeViewItem 类型:

public class Node
{
    public string Name { get; set; }
    public bool IsChecked { get; set; }
    public List<Node> Children { get; } = new List<Node>();
}

将此类型的实例添加到 TreeView

Add instance of this type to the TreeView:

public Node getTree(DirectoryInfo di)
{
    Node item = new Node();
    item.Name = di.Name;
    foreach (DirectoryInfo s in di.GetDirectories())
    {
        item.Children.Add(getTree(s));
    }
    foreach (FileInfo fi in di.GetFiles())
    {
        item.Children.Add(new Node { Name = fi.Name });
    }
    return item;
}

在视图中绑定到该类型的属性:

Bind to the properties of this type in the view:

<TreeView Name="treeView" Grid.Row="10" Grid.ColumnSpan="3">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:Node}" ItemsSource="{Binding Children}">
            <StackPanel Orientation="Horizontal">
                <CheckBox Focusable="False" IsChecked="{Binding IsChecked}" VerticalAlignment="Center"/>
                <TextBlock Text="{Binding Name}" Margin="5,0" />
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

然后可以递归地遍历项目并检查 IsChecked的值属性:

You could then iterate through the items recursively and check the value of the IsChecked property:

private List<string> GetSelectedNames()
{
    List<string> selectedNames = new List<string>();
    foreach (var item in treeView.Items.OfType<Node>())
        GetSelected(item, ref selectedNames);
    return selectedNames;
}

public void GetSelected(Node node, ref List<string> s)
{
    if (node.IsChecked)
        s.Add(node.Name);

    foreach (Node child in node.Children)
        GetSelected(child, ref s);
}

如果您想更进一步,请绑定<$ c TreeView 的$ c> ItemsSource 属性到您的 IEnumerable< Node> 属性查看模型并遍历此模型,而不是通过 TreeView 控件的 Items 属性进行迭代。

If you want to take this a step further, you bind the ItemsSource property of the TreeView to an IEnumerable<Node> property of your view model and iterate through this one instead of the Items property of the TreeView control.

这篇关于带有CheckBoxes的TreeView动态地WPF。如何知道所选元素的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆