简体   繁体   English

使用 Directory.GetDirectories(Path) 在 WPF C# 中填充 DataGrid

[英]Fill DataGrid in WPF C# with Directory.GetDirectories(Path)

I am using code like this:我正在使用这样的代码:

var s = Directory.GetDirectories(NAS PATH)

Then I fill DataGrid with it like this:然后我像这样用它填充DataGrid

RandomDataGridName.ItemsSource = s

The problem I get is that when using Listbox it works fine but once I use DataGrid I get only the length filled in DataGrid .我遇到的问题是,使用Listbox它工作正常,但是一旦我使用DataGrid我只会得到DataGrid填充的长度。

So I have done the following:所以我做了以下事情:

<DataGrid.Columns>
    <DataGridTextColumn IsReadOnly="True" Binding="{Binding }" Header="Name"/>
</DataGrid.Columns>

The above code works but I only get the name column containing the entire path of the folders in NAS.上面的代码有效,但我只得到了包含 NAS 中文件夹整个路径的名称列。

I would like to get standard date and the rest of the information that Windows keeps about its folders so users can sort directories by date.我想获得标准日期以及 Windows 保留的有关其文件夹的其余信息,以便用户可以按日期对目录进行排序。

To sum up my question:总结一下我的问题:

  1. Can DataGrid benefit from Directory.GetDirectories() ? DataGrid可以从Directory.GetDirectories()受益吗?
  2. Can I trim the DataGrid displayed path so the user does not see the full path of the directories;我可以修剪DataGrid显示的路径以便用户看不到目录的完整路径吗? rather just the last names of folders?而只是文件夹的姓氏?

you can get more info using DirectoryInfo object:您可以使用DirectoryInfo对象获取更多信息:

var folder = new DirectoryInfo(NAS_PATH);
DirectoryInfo[] subdir = folder.GetDirectories();
RandomDataGridName.ItemsSource = subdir;

note DirectoryInfo[] result (instead of string[] from Directory.GetDirectories which simply gives paths)注意DirectoryInfo[]结果(而不是DirectoryInfo[]string[]只给出路径)

then configure DataGrid to show relevant properties然后配置 DataGrid 以显示相关属性

<DataGrid.Columns>
    <DataGridTextColumn IsReadOnly="True" Binding="{Binding Name}" Header="Name"/>
    <DataGridTextColumn IsReadOnly="True" Binding="{Binding CreationTime}" Header="CreationTime"/>
</DataGrid.Columns>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM