WPF 数据模板DataTemplate、控件模板ControlTemplate、Style、ItemsPreseter

一言蔽之,Template就是"外衣"------

ControlTemplate是控件的外衣,

DataTemplate是数据的外衣。

DataTemplate 它定义了一个数据对象的可视化结构

DataTemplate常用的地方有3处,分别是:

ContentControl的ContentTemplate属性,相当于给ContentControl的内容穿衣服。

ItemsControl的 ItemTemplate属性,相当于给ItemsControl的数据条目穿衣服。

GridViewColumn的CellTemplate属性,相当于给GridViewColumn单元格里的数据穿衣服。

csharp 复制代码
 <ItemsControl ItemsSource="{Binding xxx}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition></ColumnDefinition>
                            <ColumnDefinition></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{}"></TextBlock>
                        <TextBlock Text="{}"></TextBlock>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>





Style

为了让同一种控件能担当起不同的角色,程序员就要为它们设计多种外观样式和行为动作,这就是Style。

构成Style最重要的两种元素是Setter和Trigger,Setter类帮助我们设置控件的静态外观风格,Trigger类则帮助我们设置控件的行为风格。

csharp 复制代码
<Window x:Class="Demo7._3.WpfStyle.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Demo7._3.WpfStyle"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style x:Key="myBtnStyle" TargetType="Button">
            <Style.Setters>
                <Setter Property="Background" Value="AliceBlue"/>
                <Setter Property="Height" Value="50"/>
            </Style.Setters>
        </Style>
        <Style TargetType="CheckBox">
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Trigger.Setters>
                        <Setter Property="Foreground" Value="Red"/>
                    </Trigger.Setters>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button Style="{StaticResource myBtnStyle}"/>
        <CheckBox Content="测试触发器"/>
    </StackPanel>
</Window>

》》》添加触发器

csharp 复制代码
<Window x:Class="Demo7._3.WpfStyle.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Demo7._3.WpfStyle"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style x:Key="myBtnStyle" TargetType="Button">
            <Style.Setters>
                <Setter Property="Background" Value="AliceBlue"/>
                <Setter Property="Height" Value="50"/>
            </Style.Setters>
        </Style>
      
        <Style TargetType="CheckBox">
            <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsChecked" Value="True"/>
                        <Condition Property="Content" Value="测试触发器"/>
                    </MultiTrigger.Conditions>
                    <MultiTrigger.Setters>
                        <Setter Property="Foreground" Value="Green"/>
                    </MultiTrigger.Setters>
                </MultiTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button Style="{StaticResource myBtnStyle}"/>
        <CheckBox Content="测试触发器"/>
    </StackPanel>
</Window>

》》ControlTemplate




相关推荐
△曉風殘月〆1 天前
WPF中的坐标转换
wpf
一个天蝎座 白勺 程序猿2 天前
深度解析:通过ADO.NET驱动Kdbndp高效连接与操作Kingbase数据库
数据库·.net·wpf·kingbase·金仓数据库
时光追逐者2 天前
一个使用 WPF 开发的 Diagram 画板工具(包含流程图FlowChart,思维导图MindEditor)
c#·.net·wpf·流程图
Vae_Mars2 天前
WPF中的DataTemplate
wpf
Aevget2 天前
界面控件DevExpress WPF v25.2新功能预览 - 聚焦AI功能提升
人工智能·wpf·界面控件·devexpress·ui开发·.net 10
csdn_aspnet2 天前
如何从 WPF 控件 DataGrid 中删除多余的列
wpf·datagrid
张人玉2 天前
C#WPF如何实现登录页面跳转
ui·c#·wpf
张人玉2 天前
C#WPF如何跳转页面
笔记·ui·c#·wpf
.NET修仙日记2 天前
.NET WinForms + WPF 综合学习路线:从传统到现代的.NET桌面开发
学习·c#·.net·wpf·.net core·winforms
猫林老师2 天前
鸿蒙元服务开发:免安装的卡片式服务(Atomic Service)
华为·wpf·harmonyos