更新Listview与对象数据 [英] Update Listview with Object Data

查看:140
本文介绍了更新Listview与对象数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将代码中的对象的数据放在UI上的列表视图中,但我似乎找不到正确的方法。我尝试过多次在互联网上找到,包括一些教程,但我总是碰到相同的问题。

I'm trying to put data from an object in code to a listview on the UI but I can't seems to find the right way to do it. I've tried multiple I found on the internet including some tutorial but I always run into the same issues.

大多数示例是C#,但我在VB中编写这个项目。我有C#的经验,但我不相信这是这里的问题。

Most example are in C# but I code in VB for this project. I have C# experience but I'm not convinced that's the issue here.

这是我的XAML:

<ListView x:Name="AD_AccountList" >
                <!--<ListView.ItemTemplate>
                    <DataTemplate>
                        <WrapPanel>
                            <TextBlock Text="{Binding tt1}"/>
                            <TextBlock Text="{Binding tt2}"/>
                            <TextBlock Text="{Binding tt3}"/>
                        </WrapPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>-->
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding tt1}" />
                        <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding tt2}" />
                        <GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding tt3}" />
                    </GridView>
    </ListView.View>
</ListView>

我尝试使用和不使用模板或gridview。两者都给出相同的结果:
有空行,或表填充对象名称App.testt

I tried with and without a template or gridview. Both give the same result: Either have Blank line, or the table fill with the object name "App.testt"

似乎是一个问题WPF处理数据类型的方式,但是我读到该模板应该基本上解决了这个问题。

It seems to be an issue with the way WPF handle datatype but I read that template should basically solve that issue.

我的VB代码:

Private Sub Test_Click(sender As Object, e As RoutedEventArgs)
    Dim testlist As New List(Of testt)
    testlist.Add(New testt("aaa", "bbb", "ccc"))
    testlist.Add(New testt("ddd", "fff", "ggg"))
    testlist.Add(New testt("www", "eee", "111"))

    MessageBox.Show(testlist(0).tt1)
    Dispatcher.Invoke(Sub()
                          AD_AccountList.ItemsSource = testlist
                      End Sub)
    End Sub
End Class

Public Class testt
    Public tt1 As String
    Public tt2 As String
    Public tt3 As String

    Sub New(tt1 As String, tt2 As String, tt3 As String)
        Me.tt1 = tt1
        Me.tt2 = tt2
        Me.tt3 = tt3
   End Sub
End Class

消息框用于测试,它确实显示了我要求的数据,所以我的对象被填满了数据。

The messagebox was for testing and it indeed show me the data I request for, so my object is filled up with data for sure.

我是WPF的新手。我正在将一个软件从win32移植到WPF,但是我不想这么简单,所以我想在WPF中如何处理这个问题。

I'm pretty new to WPF. I'm am moving a software from win32 to WPF but I don't want to do this the easy way so I would like the real way to handle this in WPF if possible.

我不介意链接到可以帮助或引用的东西,但是在我的研究中我已经用完了。

I don't mind a link to something that can help or reference but I ran out of that in my research.

regards,

推荐答案

您不能绑定到XAML中的字段,只能绑定到属性。 testt 有字段而不是属性。

You can't bind to fields in XAML, only to properties. testt has fields, not properties.

尝试将字段更改为自动实现的属性:

Try changing the fields to auto-implemented properties:

Public Property tt1 As String
Public Property tt2 As String
Public Property tt3 As String

这篇关于更新Listview与对象数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆