WPF DataGrid Row 添加到代码隐藏中

发布于 2024-09-10 14:33:06 字数 1132 浏览 1 评论 0原文

我是从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放血 2024-09-17 14:33:06

您好:您应该设置 ItemsSource 而不是手动添加项目。如果列设置正确,那么它就会“工作”!

dbv.ItemsSource = Base.GetFiles

或者

dbv.ItemsSource = CreateMyFileInfos(Base.GetFiles)

如果您还有任何问题,请发回此处。

编辑:在第二次检查时,看起来您可能想要递归地执行此操作。在这种情况下,您的 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'!

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!

你的笑 2024-09-17 14:33:06

WPF是一种思维方式的改变,你需要摆脱Winforms的思维方式。

最终,您需要将 ItemsSource 设置为 IEnumerable,最好是 ObservableCollection。

最快的开始方法是将 ObservableCollection 作为公共属性放在代码隐藏文件中:

public ObservableCollection<DirectoryInfo> files { get;set; }

然后在构造函数或窗口上的 Load 事件中,用您的数据填充集合,然后添加到您的 Xaml 声明中DataGrid:

ItemsSource = "{Binding Path=files}"

编辑:

我使用 DirectoryInfo 类进行了尝试,在后面的代码中我添加了:

    public ObservableCollection<DirectoryInfo> Dir = new ObservableCollection<DirectoryInfo>();
    public Window1()
    {
        InitializeComponent();
        Dir.Add(new DirectoryInfo("c:\\"));
        Dir.Add(new DirectoryInfo("c:\\temp\\"));
        dataGrid1.ItemsSource = Dir;
    }

出于某种原因,通过 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:

public ObservableCollection<DirectoryInfo> files { get;set; }

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:

ItemsSource = "{Binding Path=files}"

EDIT:

I tried this out using the DirectoryInfo class, in my code behind I added:

    public ObservableCollection<DirectoryInfo> Dir = new ObservableCollection<DirectoryInfo>();
    public Window1()
    {
        InitializeComponent();
        Dir.Add(new DirectoryInfo("c:\\"));
        Dir.Add(new DirectoryInfo("c:\\temp\\"));
        dataGrid1.ItemsSource = Dir;
    }

For some reason this was not working using the Databinding via Xaml, but I did not try very hard to get it to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文