WPF DataGrid行添加codebehind [英] WPF DataGrid Row add in codebehind
问题描述
请帮助我选择正确的方法来获取我的文件名,状态文本和缩略图添加到DataGrid行?
在VB.Net WinForms中,我可以添加如下一行:
Datagridview1.Rows.add Myvalue,RowStateText,Hello World,MyDate)
在WPF的DataGrid中,我可以添加
DataGrid1.Items.Add(New DataGridRow())
但是如何填写我的DataGridRow?
Private Sub AddFilesAndFolders(ByVal Base as IO.DirectoryInfo,ByRef dgv As DataGrid)
'为每个di为IO.DirectoryInfo在Base.GetDirectories
'调用AddFilesAndFolders(di,dgv)
'下一个
Dim item As DataGridRow
For each fi As IO.FileInfo In Base.GetFiles
item = New DataGridRow'< - test 1(row is添加但空)
Dim di As New MyFileInfo < - test 2(我自己的类与公共成员,但如何添加为行与声明的列?)
di.FileName = fi.FullName
di.FileDate = fi.LastAccessTime
item.Item = fi.FullName
dgv.Items.Add(di)
下一个
End Sub
你应该设置一个ItemsSource而不是手动添加项目。如果这些列设置正确,那么它只会'工作'!
dbv.ItemsSource = Base.GetFiles
或
dbv.ItemsSource = CreateMyFileInfos(Base.GetFiles)
如果您有任何更多问题,请回到这里。
编辑:在第二次检查中,您可能希望以递归方式执行。在这种情况下,您的 AddFilesAndFolders
可以替代为 CreateFilesAndFolders
,这将返回FileInfo / MyFileInfo对象的集合,并与递归生成子文件夹的集合;然后绑定从第一个调用返回的整个列表到网格。
希望有帮助!
I am from VB.Net WinForms comming. Now I wanted to write a small app in WPF, listing some files in a datagridview. I used WPF's DataGrid, created some Columns. And then failed to add my rows.
Please, can you help me to select the right way to get my filenames, state-text and thumbnails added to the DataGrid Row?
In VB.Net WinForms I can add a row like this:
Datagridview1.Rows.add(Myvalue, "RowStateText", "Hello World", MyDate)
In WPF's DataGrid I can add
DataGrid1.Items.Add(New DataGridRow())
But how to fill my DataGridRow?
Private Sub AddFilesAndFolders(ByVal Base As IO.DirectoryInfo, ByRef dgv As DataGrid)
'For Each di As IO.DirectoryInfo In Base.GetDirectories
' Call AddFilesAndFolders(di, dgv)
'Next
Dim item As DataGridRow
For Each fi As IO.FileInfo In Base.GetFiles
item = New DataGridRow'<-- test 1 (row is added but empty)
Dim di As New MyFileInfo'<-- test 2 (my own class with public members, but how to add as row with declared columns?)
di.FileName = fi.FullName
di.FileDate = fi.LastAccessTime
item.Item = fi.FullName
dgv.Items.Add(di)
Next
End Sub
Hi: you should set an ItemsSource instead of adding items manually. If the columns are set up correctly then it will just 'work'!
dbv.ItemsSource = Base.GetFiles
or
dbv.ItemsSource = CreateMyFileInfos(Base.GetFiles)
If you have any more problems, please post back here.
Edit: on second inspection it looks like you may want to be doing it recursively. In which case your AddFilesAndFolders
could instead be CreateFilesAndFolders
, which would return a collection of FileInfo/MyFileInfo objects, merged with the collections produced by the child folders recursively; then bind the whole list returned from the first call, to the grid.
Hope that helps!
这篇关于WPF DataGrid行添加codebehind的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!