Use Data Templates to Display Bound Data : ListBox « Windows Presentation Foundation « VB.Net Tutorial






<Window 
    x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfApplication1="clr-namespace:WpfApplication1"
    Title="WPF" Height="300" Width="300">

    <Window.Resources>
        <WpfApplication1:People x:Key="people"/>
        
        <DataTemplate x:Key="personTemplate">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="80"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <StackPanel>
                        <TextBlock
                            Style="{StaticResource lblStyle}"
                            Text="First Name" />
                        <TextBlock 
                            Style="{StaticResource dataStyle}"
                            Text="{Binding Path=FirstName}"/>
                        <TextBlock 
                            Style="{StaticResource lblStyle}"
                            Text="Age" />
                        <TextBlock 
                            Style="{StaticResource dataStyle}"
                            Text="{Binding Path=Age}" />
                    </StackPanel>

                    <Image 
                        Margin="4"
                        Grid.Column="1" 
                        Width="96"
                        Height="140"
                        Source="{Binding Path=Photo}"/>
                </Grid>
        </DataTemplate>


    </Window.Resources>

    <Grid>
        <ListBox
            ItemsSource="{Binding Source={StaticResource people}}"
            ItemTemplate="{StaticResource personTemplate}"/>


        <ListBox
            Margin="10"
            ItemsSource="{Binding Source={StaticResource people}}"/>
    </Grid>
</Window>
//File:Window.xaml.vb
Imports System.Collections.ObjectModel

Namespace WpfApplication1
  Public Class Employee
    Public Property FirstName() As String
      Get
        Return m_FirstName
      End Get
      Set
        m_FirstName = Value
      End Set
    End Property
    Private m_FirstName As String

    Public Property Age() As Integer
      Get
        Return m_Age
      End Get
      Set
        m_Age = Value
      End Set
    End Property
    Private m_Age As Integer
    Public Property Photo() As String
      Get
        Return m_Photo
      End Get
      Set
        m_Photo = Value
      End Set
    End Property
    Private m_Photo As String

    Public Overrides Function ToString() As String
      Return FirstName
    End Function
  End Class

  Public Class People
    Inherits Collection(Of Employee)
    Public Sub New()
      Me.Add(New Employee() With { _
        .FirstName = "A", _
        .Age = 26, _
        .Photo = "a.png" _
      })
      Me.Add(New Employee() With { _
        .FirstName = "C", _
        .Age = 24, _
        .Photo = "c.png" _
      })
    End Sub
  End Class
End Namespace








16.27.ListBox
16.27.1.Bind String Array Resource to ListBoxBind String Array Resource to ListBox
16.27.2.Use Data Templates to Display Bound DataUse Data Templates to Display Bound Data
16.27.3.CheckBox ListCheckBox List
16.27.4.Convert the contents of a ListBoxItem to an instance of GridLength by using GridLengthConverterConvert the contents of a ListBoxItem to an instance of GridLength by using GridLengthConverter
16.27.5.Use the FontSizeConverter class to convert the content of a ListBoxItem to a value that represents the size of a font.Use the FontSizeConverter class to convert the content of a ListBoxItem to a value that represents the size of a font.
16.27.6.Convert contents of a ListBoxItem to an instance of Thickness by using the ThicknessConverterConvert contents of a ListBoxItem to an instance of Thickness by using the ThicknessConverter
16.27.7.DataTrigger, ListBox and user objectDataTrigger, ListBox and user object
16.27.8.Add selected file to ListBoxAdd selected file to ListBox
16.27.9.This list box allows items to be selected in groups by using the SHIFT key and mouse or the CTRL key and space key.This list box allows items to be selected in groups by using the SHIFT key and mouse or the CTRL key and space key.
16.27.10.Iterate through the selected items and remove each oneIterate through the selected items and remove each one
16.27.11.Ensure there is at least one item selectedEnsure there is at least one item selected