如何获取TreeViewItems的已检查复选框的文件名

时间:2016-06-09 00:48:32

标签: c# wpf checkbox nodes treeviewitem

我的任务是创建一个程序,它接受一个路径并显示目录,包括带有文件扩展名过滤器的文件。然后应该读取文件并且必须计算它们的行(LOC)。只能读取已检查的文件。

我使用TreeViewItem显示文件并在每个文件的标题中添加了一个CheckBox。我还没弄清楚如何确定文件是否已被检查。我已经看过堆栈溢出但找不到合适的东西。

所以基本上我需要获取已检查的文件名以读出它们包含的行数。

涉及方法:

private void ListDirectory(System.Windows.Controls.TreeView treeView, string path)
{
    try
    {
        if (Directory.Exists(path))
        {
            treeView.Items.Clear();
            DirectoryInfo rootDirectoryInfo = new DirectoryInfo(path);
            treeView.Items.Add(CreateDirectoryNode(rootDirectoryInfo));
        }
        else
        {
            throw new PathException(path);
        }
    }
    catch (PathException pExc)
    {
        System.Windows.MessageBox.Show(pExc.ToString(), "", MessageBoxButton.OK, MessageBoxImage.Exclamation);
    }
}

private TreeViewItem CreateDirectoryNode(DirectoryInfo directoryInfo)
{
    TreeViewItem directoryNode = new TreeViewItem {
        Header = directoryInfo.Name
    };

    directoryNode.Foreground = Brushes.BlueViolet;
    foreach (var directory in directoryInfo.GetDirectories())
    {
        try
        {
            directoryNode.Items.Add(CreateDirectoryNode(directory));
        }
        catch (UnauthorizedAccessException)
        {
            System.Windows.MessageBox.Show("Certain files could not be opened", "", MessageBoxButton.OK, MessageBoxImage.Information);
            break;
        }
    }

    var cbox_ex_array = comboBox_extensions.SelectedValue.ToString().Split(' ');
    foreach (var file in directoryInfo.GetFiles(cbox_ex_array[1]))
    {
        TreeViewItem ti = new TreeViewItem {
            Header = new System.Windows.Controls.CheckBox() {
                        IsChecked = true, Content = file.Name
            }
        };

        ti.Foreground = Brushes.Red;
        directoryNode.Items.Add(ti);
    }

    return directoryNode;
}

private void button_display_Click(object sender, RoutedEventArgs e)
{
    ListDirectory(treeView, textBox_path.Text);
}

private void button_get_Click(object sender, RoutedEventArgs e)
{
    //Here I'd like to gather all checked files in form of a list/array
}

目前的情况如下: application

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,使用 ItemCollection 并将其项目强制转换为 TreeViewItem ,然后询问标题是否为 CheckBox

private static Dictionary<int, string> filesToRead = new Dictionary<int, string>();

private void button_get_Click(object sender, RoutedEventArgs e)
    {
        if (filesToRead.Count > 0)
        {
            filesToRead.Clear();
        }

        GetCheckedItems(treeView.Items);

        if (filesToRead.Count > 0)
        {
            label_files_r.Visibility = Visibility.Visible;
            label_files_r.Content = "files received: " + filesToRead.Count.ToString();
        }
        else
        {
            label_files_r.Visibility = Visibility.Hidden;
            System.Windows.MessageBox.Show("No files found!", "Attention", MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }
    }

    private void GetCheckedItems(ItemCollection collection)
    {
        foreach (var itemRaw in collection)
        {
            var item = (TreeViewItem)itemRaw;

            if (item.Header is System.Windows.Controls.CheckBox)
            {
                var checkBox = (System.Windows.Controls.CheckBox)item.Header;
                if (checkBox.IsChecked == true)
                    filesToRead.Add((filesToRead.Count + 1), checkBox.Content.ToString());
            }
            else
            {
                GetCheckedItems(item.Items);
            }
        }
    }