wpf 列出 DataGrid 中的目录文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18474761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List directory files in a DataGrid
提问by MCSharp
I have searched many topics and can't find an answer on using the WPF DataGridto list file name contents from a directory. I am able to output the contents in a ListBoxbut have no idea how to add items to a Columnin DataGrid.
我搜索了许多主题,但找不到有关使用 WPFDataGrid从目录中列出文件名内容的答案。我能够输出 a 中的内容,ListBox但不知道如何将项目添加到 aColumn中DataGrid。
This works for a ListBox
这适用于 ListBox
string path = "C:";
object[] AllFiles = new DirectoryInfo(path).GetFiles().ToArray();
foreach (object o in AllFiles)
{
listbox.Items.Add(o.ToString());
}
How can I do the same with a DataGrid? Or atleast place stringsfrom an arrayinto a DataGridColumn?
我怎样才能对 a 做同样的事情DataGrid?或者至少strings从一个地方array变成一个DataGridColumn?
回答by dkozl
You can create DataGridwith one column:
您可以DataGrid使用一列创建:
<DataGrid x:Name="myDataGrid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True" Binding="{Binding}" Header="Name"/>
</DataGrid.Columns>
</DataGrid>
and fill it in your code like this:
并像这样填写您的代码:
myDataGrid.ItemsSource = new DirectoryInfo(path).GetFiles();
By setting ItemsSourceto FileInfo[]you have option to create other columns bound to other properties for FileInfoclass. This DataGridwill work with any IEnumerableassigned to ItemsSource. If it won't be a stringalready then ToString()will be called
通过设置ItemsSource为FileInfo[]您可以选择为类创建绑定到其他属性的其他列FileInfo。这DataGrid将适用于任何IEnumerable分配给ItemsSource. 如果它不是string已经那么ToString()将被调用
回答by Guga
You first have to add Columns in your DataGrid (using VS is pretty simple with the designer) and then you can use something like:
您首先必须在 DataGrid 中添加列(使用 VS 对设计器来说非常简单),然后您可以使用以下内容:
for (int i = 0; i < Object.Length; i++)
dataGrid.Rows[i].Cells[0].Value = Object[i];
In this case i'm using Cells[0], but you can specify any cell on your row to put the values.
在这种情况下,我使用 Cells[0],但您可以指定行中的任何单元格来放置值。
回答by Bit
You should be able to bind your listbox to the DataGrid something like:
您应该能够将列表框绑定到 DataGrid,例如:
<Window x:Class="Bind02.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Bind02" Height="300" Width="300"
>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ListBox Name="listBox" ItemsSource="{Binding}"/>
<StackPanel Grid.Column="1">
<Button Click="OnLoad">_Load</Button>
<Button Click="OnSave">_Save</Button>
<Button Click="OnAdd">_Add</Button>
<Button Click="OnEdit">_Edit</Button>
<Button Click="OnDelete">_Delete</Button>
<Button Click="OnExit">E_xit</Button>
</StackPanel>
</Grid>
</Window>
回答by Hymanson
Instead of:
代替:
object[] AllFiles = new DirectoryInfo(path).GetFiles().ToArray();
use
用
List<string> AllFiles = new DirectoryInfo(path).GetFiles().ToList();
MyDataGrid.ItemSource = Allfiles;
This will automatically bind the files to the DataGrid.
这将自动将文件绑定到 DataGrid。
回答by Raj
string [] fileEntries = Directory.GetFiles(targetDirectory);
List<FileInfo> fileList = new List<FileInfo>();
foreach (string file in fileEntries)
{
fileList.Add(new FileInfo(file));
}
datagrid.ItemsSource = fileList;

