Defines the contents of column headers and cells by using templates. : DataTemplate « Windows Presentation Foundation « VB.Net

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Development
8.Event
9.File Directory
10.Generics
11.GUI
12.Language Basics
13.LINQ
14.Network Remote
15.Security
16.Thread
17.Windows Presentation Foundation
18.Windows System
19.XML
20.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » Windows Presentation Foundation » DataTemplateScreenshots 
Defines the contents of column headers and cells by using templates.
Defines the contents of column headers and cells by using templates.
      

<Window x:Class="WpfApplication1.Window1"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ds="clr-namespace:WpfApplication1">
  <Window.Resources>
    <ObjectDataProvider x:Key="myDateCollectionDataSource" ObjectType="{x:Type ds:myDateCollection}"/>
    <Style x:Key="GridViewColumnHeaderGripper" TargetType="{x:Type Thumb}">
      <Setter Property="Height" Value="{Binding Path=ActualHeight,RelativeSource={RelativeSource TemplatedParent}}"/>
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type Thumb}">
            <Border>
              <Rectangle HorizontalAlignment="Center" Width="1" Fill="Black"/>
            </Border>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
    <Style x:Key="myControlTemplateStyle" TargetType="{x:Type GridViewColumnHeader}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
            <Grid Background="LightBlue">
              <DockPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                <CheckBox></CheckBox>
                <TextBlock Text="{TemplateBinding Content}" FontSize="16" Foreground="DarkBlue"/>
              </DockPanel>
              <Canvas>
              <Thumb x:Name="PART_HeaderGripper"
                     Style="{StaticResource GridViewColumnHeaderGripper}"
                     Background="Transparent"
                     />
            </Canvas>
            </Grid>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>

    <Style x:Key="myHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
      <Setter Property="Background" Value="LightBlue"/>
    </Style>

    <DataTemplate x:Key="myHeaderTemplate">
      <DockPanel>
        <CheckBox/>
        <TextBlock FontSize="16" Foreground="DarkBlue">
          <TextBlock.Text>
            <Binding/>
          </TextBlock.Text>
        </TextBlock>
      </DockPanel>
    </DataTemplate>

    <DataTemplate x:Key="myCellTemplateDay">
      <DockPanel>
        <TextBlock Foreground="DarkBlue" HorizontalAlignment="Center">
          <TextBlock.Text>
            <Binding Path="Day"/>
          </TextBlock.Text>
        </TextBlock>
      </DockPanel>
    </DataTemplate>

    <DataTemplate x:Key="myCellTemplateMonth">
      <DockPanel>
        <TextBlock Foreground="DarkBlue" HorizontalAlignment="Center">
          <TextBlock.Text>
            <Binding Path="Month"/>
          </TextBlock.Text>
        </TextBlock>
      </DockPanel>
    </DataTemplate>
    <DataTemplate x:Key="myCellTemplateYear">
      <DockPanel>
        <TextBlock Foreground="DarkBlue" HorizontalAlignment="Center">
          <TextBlock.Text>
            <Binding Path="Year"/>
          </TextBlock.Text>
        </TextBlock>
      </DockPanel>
    </DataTemplate>
  </Window.Resources>
  <StackPanel>
      <ListView ItemsSource="{Binding Source={StaticResource myDateCollectionDataSource}}"
                HorizontalAlignment="Center">
        <ListView.View>
          <GridView>
            <GridViewColumn Header="Year" Width="80"
                  HeaderContainerStyle="{StaticResource myHeaderStyle}"
                  HeaderTemplate="{StaticResource myHeaderTemplate}"
                  CellTemplate="{StaticResource myCellTemplateYear}"/>
            <GridViewColumn Header="Month" Width="80"
                  HeaderContainerStyle="{StaticResource myHeaderStyle}"
                  HeaderTemplate="{StaticResource myHeaderTemplate}"
                  DisplayMemberBinding="{Binding Path=Month}"/>
            <GridViewColumn Header="Day" Width="80"
                  HeaderContainerStyle="{StaticResource myHeaderStyle}"
                  HeaderTemplate="{StaticResource myHeaderTemplate}"
                  CellTemplate="{StaticResource myCellTemplateDay}"/>
          </GridView>
        </ListView.View>
      </ListView>
  </StackPanel>

</Window>
//File:Window.xaml.vb
Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Media
Imports System.Windows.Shapes
Imports System.Windows.Controls.Primitives
Imports System.Collections.ObjectModel

Namespace WpfApplication1
  Public Partial Class Window1
    Inherits Window
  End Class

  Public Class myDateCollection
    Inherits ObservableCollection(Of DateTime)
    Public Sub New()
      Add(New DateTime(200511))
      Add(New DateTime(200481))
      Add(New DateTime(2003124))
      Add(New DateTime(2004218))
      Add(New DateTime(2004630))
    End Sub
  End Class
End Namespace

   
    
    
    
    
    
  
Related examples in the same category
1.Use DataTemplate in ListBoxUse DataTemplate in ListBox
2.ListBox binds to the people collection, and sets the DataTemplate to use for displaying each item
3.Without specifying a DataTemplate, the ListBox displays a list of names.Without specifying a DataTemplate, the ListBox displays a list of names.
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.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.