我搜索了很多主题,但找不到有关使用 WPF 的答案DataGrid
列出目录中的文件名内容。我能够输出内容ListBox
但不知道如何将项目添加到Column
in DataGrid
.
这适用于ListBox
string path = "C:";
object[] AllFiles = new DirectoryInfo(path).GetFiles().ToArray();
foreach (object o in AllFiles)
{
listbox.Items.Add(o.ToString());
}
我怎样才能做同样的事DataGrid
?或者至少有一个地方strings
从一个array
into a DataGrid
Column
?
您可以创建DataGrid
一列:
<DataGrid x:Name="myDataGrid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True" Binding="{Binding}" Header="Name"/>
</DataGrid.Columns>
</DataGrid>
并将其填充到您的代码中,如下所示:
myDataGrid.ItemsSource = new DirectoryInfo(path).GetFiles();
通过设置ItemsSource
to FileInfo[]
您可以选择创建绑定到其他属性的其他列FileInfo
班级。这DataGrid
将与任何IEnumerable
分配给ItemsSource
。如果它不会是一个string
那时已经ToString()
将被称为
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)