这是listview的xaml
<ListView Grid.Column="1" Height="auto" Name="ListView1" Width="auto" AllowDrop ="True" >
<ListView.View>
<GridView >
<GridViewColumn Header="File Name" />
<GridViewColumn Header="Path" />
<GridViewColumn Header="type" />
</GridView>
</ListView.View>
</ListView>
我在列表框中添加了一个项目,但它添加了一个具有三个相同值的行
ListView1.Items.Add("abcd")
我也尝试了一些方法,但仍有问题
所以我想知道如何为第二列添加不同的值
Dim x As ItemCollection
x.Add("a")
x.Add("b")
x.Add("c")
ListView1.Items.Add(x)
Dim x As New ItemCollection
x.Add("a")
x.Add("b")
x.Add("c")
ListView1.Items.Add(x)
如何获得第二列的值?
我试过这段代码,但它只返回第一列的第二个字符,我找不到任何方法来访问第二列
ListView1.Items(0)(1)
答案 0 :(得分:1)
我认为你需要绑定列表才能发挥作用。
我制作了这个示例类:
Public Class ListViewItemTemplate
Public Property FileName As String
Public Property FilePath As String
Public Property FileType As String
End Class
更改了xaml以包含DisplayMember绑定:
<ListView Name="ListView1" Width="auto" AllowDrop ="True" Margin="0,0,0,41">
<ListView.View>
<GridView >
<GridViewColumn Header="File Name" DisplayMemberBinding="{Binding Path=FileName}" />
<GridViewColumn Header="Path" DisplayMemberBinding="{Binding Path=FilePath}"/>
<GridViewColumn Header="type" DisplayMemberBinding="{Binding Path=FileType}" />
</GridView>
</ListView.View>
</ListView>
并加载了一些示例数据:
Dim itemsList As New List(Of ListViewItemTemplate)
Dim item As New ListViewItemTemplate
item.FileName = "FileName A"
item.FilePath = "FilePath A"
item.FileType = "FileType A"
itemsList.Add(item)
item = New ListViewItemTemplate
item.FileName = "FileName B"
item.FilePath = "FilePath B"
item.FileType = "FileType B"
itemsList.Add(item)
item = New ListViewItemTemplate
item.FileName = "FileName C"
item.FilePath = "FilePath C"
item.FileType = "FileType C"
itemsList.Add(item)
ListView1.ItemsSource = itemsList
祝你好运!!