タグ: WPF ListView 追加

配列を追加する

example (XAML)

<ListView Name="ListView1">
  <ListView.View>
    <GridView>
      <GridViewColumn Header="Col1" DisplayMemberBinding="{Binding [0]}" />
      <GridViewColumn Header="Col2" DisplayMemberBinding="{Binding [1]}" />
      <GridViewColumn Header="Col3" DisplayMemberBinding="{Binding [2]}" />
   </GridView>
  </ListView.View>
<ListView>

example (VB.net)

Sub Main()
  ListView1.Items.Add(New String() {"Row1", "123", "4567"})
  ListView1.Items.Add(New String() {"Row2", "123", "4567"})
  ListView1.Items.Add(New String() {"Row3", "123", "4567"})
  ListView1.Items.Add(New String() {"Row4", "123", "4567"})
End Sub

オブジェクトの配列を追加する

example (XAML)

<Window x:Class="MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="MainWindow" Height="350" Width="525">
  <Grid>
    <ListView Name="ListView1">
      <ListView.View>
        <GridView>
          <GridViewColumn DisplayMemberBinding="{Binding Path=Code}" 
                          Header="Code"/>
          <GridViewColumn DisplayMemberBinding="{Binding Path=Name}" 
                          Header="Name"/>
        </GridView>
      </ListView.View>
    </ListView>
  </Grid>
</Window>

example (VB.net)

Class MainWindow 

	'[Itemクラス]
	Public Class Item
		Sub New(ByVal val1 As String, ByVal val2 As String)
			Me._Name = val1
			Me._Code = val2
		End Sub
		Public Property Name As String
		Public Property Code As String
	End Class

	Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
		Dim data(1) As Item
		data(0) = New Item("item1", "x456")
		data(1) = New Item("item2", "x567")
		ListView1.ItemsSource = data
	End Sub
End Class