代码改变世界

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

  敏捷的水  阅读(1703)  评论(2编辑  收藏  举报

WPF模版主要分为俩大类:

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

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

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

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

2. 示例

ItemsControl

image

image

image

ContentControl

image

image

 

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>

 

image

 

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>

image

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
历史上的今天:
2008-02-27 ten sentences(91-100)
2008-02-27 ten sentences(81-90)
2008-02-27 ten sentences(71-80)
2008-02-27 ten sentences(61-70)
2008-02-27 ten sentences(51-60)
点击右上角即可分享
微信分享提示