WPF DataGrid Row 添加到代码隐藏中
我是从VB.Net WinForms 过来的。现在我想在 WPF 中编写一个小应用程序,在 datagridview 中列出一些文件。我使用 WPF 的 DataGrid,创建了一些列。然后无法添加我的行。
您能帮助我选择正确的方式将我的文件名、状态文本和缩略图添加到 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)
'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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您好:您应该设置 ItemsSource 而不是手动添加项目。如果列设置正确,那么它就会“工作”!
或者
如果您还有任何问题,请发回此处。
编辑:在第二次检查时,看起来您可能想要递归地执行此操作。在这种情况下,您的
AddFilesAndFolders
可以改为CreateFilesAndFolders
,它将返回 FileInfo/MyFileInfo 对象的集合,并与子文件夹递归生成的集合合并;然后将从第一次调用返回的整个列表绑定到网格。希望有帮助!
Hi: you should set an ItemsSource instead of adding items manually. If the columns are set up correctly then it will just 'work'!
or
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 beCreateFilesAndFolders
, 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是一种思维方式的改变,你需要摆脱Winforms的思维方式。
最终,您需要将 ItemsSource 设置为 IEnumerable,最好是 ObservableCollection。
最快的开始方法是将 ObservableCollection 作为公共属性放在代码隐藏文件中:
然后在构造函数或窗口上的 Load 事件中,用您的数据填充集合,然后添加到您的 Xaml 声明中DataGrid:
编辑:
我使用 DirectoryInfo 类进行了尝试,在后面的代码中我添加了:
出于某种原因,通过 Xaml 使用数据绑定无法正常工作,但我并没有非常努力地让它工作。
WPF is a mindset change, you need to get away from the Winforms way of thinking.
Ultimately you need to set the ItemsSource to an IEnumerable, preferably a ObservableCollection.
The quickest way to get started would be to put the ObservableCollection as a public property in your code-behind file:
Then in the constructor or a Load event on the Window, populate the collection with your data and then add to the Xaml declaration for your DataGrid:
EDIT:
I tried this out using the DirectoryInfo class, in my code behind I added:
For some reason this was not working using the Databinding via Xaml, but I did not try very hard to get it to work.