在DataGrid清单目录中的文件 [英] List directory files in a DataGrid
问题描述
我已经搜查了许多话题和无法找到使用WPF 的DataGrid
列出从目录中的文件名内容的答案。我能够输出一个列表框
的内容,但不知道如何将项目添加到列
在的DataGrid
。
I have searched many topics and can't find an answer on using the WPF DataGrid
to list file name contents from a directory. I am able to output the contents in a ListBox
but have no idea how to add items to a Column
in DataGrid
.
这适用于一个列表框
string path = "C:";
object[] AllFiles = new DirectoryInfo(path).GetFiles().ToArray();
foreach (object o in AllFiles)
{
listbox.Items.Add(o.ToString());
}
我如何可以做同样的用的DataGrid
?或至少替换字符串
从阵列
到的DataGrid
列
?
How can I do the same with a DataGrid
? Or atleast place strings
from an array
into a DataGrid
Column
?
推荐答案
您可以创建的DataGrid
有一列:
<DataGrid x:Name="myDataGrid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True" Binding="{Binding}" Header="Name"/>
</DataGrid.Columns>
</DataGrid>
和填充它在你的code是这样的:
and fill it in your code like this:
myDataGrid.ItemsSource = new DirectoryInfo(path).GetFiles();
通过设置的ItemsSource
到的FileInfo []
你必须选择创建绑定到其他属性的其他列的FileInfo
类。这的DataGrid
将与任何的IEnumerable
分配给的ItemsSource
。如果它不会是一个字符串
已经再的ToString()
将被称为
By setting ItemsSource
to FileInfo[]
you have option to create other columns bound to other properties for FileInfo
class. This DataGrid
will work with any IEnumerable
assigned to ItemsSource
. If it won't be a string
already then ToString()
will be called
这篇关于在DataGrid清单目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!