Without specifying a DataTemplate, the ListBox displays a list of names. : DataTemplate « Windows Presentation Foundation « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Windows Presentation Foundation » DataTemplate 
16.97.3.Without specifying a DataTemplate, the ListBox displays a list of names.
<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
            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
WPF Without Specifying A Data Template The List Box Displays A List Of Names
16.97.DataTemplate
16.97.1.Use DataTemplate in ListBoxUse DataTemplate in ListBox
16.97.2.ListBox binds to the people collection, and sets the DataTemplate to use for displaying each itemListBox binds to the people collection, and sets the DataTemplate to use for displaying each item
16.97.3.Without specifying a DataTemplate, the ListBox displays a list of names.Without specifying a DataTemplate, the ListBox displays a list of names.
16.97.4.Enables sorting of data in ascending or descending order according to the contents of one column.Enables sorting of data in ascending or descending order according to the contents of one column.
16.97.5.Defines the contents of column headers and cells by using templates.Defines the contents of column headers and cells by using templates.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.