且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无废话WPF系列17:数据模版

更新时间:2022-09-13 20:21:07

WPF模版主要分为俩大类:

ControlTemplate: 控件的外观,也就是控件是什么样子

DataTemplate: 是数据内容的表现,一条数据显示成什么样子

1. 数据模版常用的地方有以下几处:

  • ContentControl的ContentTemplate属性。
  • ItemsControl的ItemTemplate属性。
  • GridViewColumn的CellTemplate属性。

2. 示例

ItemsControl

无废话WPF系列17:数据模版

无废话WPF系列17:数据模版

无废话WPF系列17:数据模版

ContentControl

无废话WPF系列17:数据模版

无废话WPF系列17:数据模版

 

3. DataTemplate除了可以作用在控件上,也可以作用再数据类型上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<Window x:Class="DeepXAML.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DeepXAML"      
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
         xmlns:cl="clr-namespace:System.Collections;assembly=mscorlib"
        Title="MainWindow" Height="250" Width="450">
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:Student}">           
            <StackPanel Orientation="Horizontal" >
            <Grid Margin="5">               
                <Rectangle Fill="YellowGreen"  Width="{Binding  Path=Score}"/>
                <TextBlock  Text="{Binding Path=Name}"></TextBlock>
            </Grid>
                <TextBlock Text="{Binding Path=Score}" Margin="5"></TextBlock>
            </StackPanel>
        </DataTemplate>
        <cl:ArrayList x:Key="allStudentsList">
            <local:Student Name="Jack" Gender="True" Score="80"></local:Student>
            <local:Student Name="Tom" Gender="False" Score="40"></local:Student>
            <local:Student Name="Jack" Gender="True" Score="75"></local:Student>
        </cl:ArrayList>
    </Window.Resources>
    <StackPanel x:Name="stackPanel">
        <ListBox ItemsSource="{StaticResource ResourceKey=allStudentsList}" FontSize="15"></ListBox>
        <TextBlock Margin="10">Below is combox</TextBlock>
        <ComboBox ItemsSource="{StaticResource ResourceKey=allStudentsList}" FontSize="15"></ComboBox>
    </StackPanel>
</Window>

 

无废话WPF系列17:数据模版

 

4. DataTemplate作用在XML元素上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<Window x:Class="DeepXAML.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DeepXAML"      
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
         xmlns:cl="clr-namespace:System.Collections;assembly=mscorlib"
        Title="MainWindow" Height="250" Width="450">
    <Window.Resources>
        <DataTemplate DataType="Student">           
            <StackPanel Orientation="Horizontal" >
            <Grid Margin="5">               
                <Rectangle Fill="YellowGreen"  Width="{Binding  XPath=@Score}"/>
                <TextBlock  Text="{Binding XPath=@Name}"></TextBlock>
            </Grid>
                <TextBlock Text="{Binding XPath=@Score}" Margin="5"></TextBlock>
            </StackPanel>
        </DataTemplate>
        <XmlDataProvider x:Key="xmlDp" XPath="Students/Student">
            <x:XData>
                <Students xmlns="">
                    <Student Name="Jack" Score="80"></Student>
                    <Student Name="Tom" Score="40"></Student>
                    <Student Name="David" Score="75"></Student>
                </Students>
            </x:XData>
        </XmlDataProvider>
    </Window.Resources>
    <StackPanel x:Name="stackPanel">
        <ListBox ItemsSource="{Binding Source={StaticResource xmlDp}}" FontSize="15"></ListBox>
        <TextBlock Margin="10">Below is combox</TextBlock>
        <ComboBox ItemsSource="{Binding Source={StaticResource xmlDp}}" FontSize="15"></ComboBox>
    </StackPanel>
</Window>

无废话WPF系列17:数据模版

本文转自敏捷的水博客园博客,原文链接http://www.cnblogs.com/cnblogsfans/archive/2011/02/27/1966172.html如需转载请自行联系原作者


王德水