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




相关推荐
FuckPatience12 小时前
WPF Telerik.Windows.Controls.Data.PropertyGrid 自定义属性编辑器
wpf
almighty2718 小时前
C#WPF控制USB摄像头参数:曝光、白平衡等高级设置完全指南
开发语言·c#·wpf·usb相机·参数设置
军训猫猫头1 天前
12.NModbus4在C#上的部署与使用 C#例子 WPF例子
开发语言·c#·wpf
我要打打代码1 天前
在WPF项目中使用阿里图标库iconfont
wpf
拾忆,想起2 天前
Redisson 分布式锁的实现原理
java·开发语言·分布式·后端·性能优化·wpf
weixin_464078072 天前
wpf依赖注入驱动的 MVVM实现(含免费源代码demo)
wpf
beyond谚语2 天前
一、WPF入门介绍+Grid和StackPanel布局介绍+实战模拟Notepad++页面布局
wpf
CPU不够了2 天前
WPF常见问题清单
wpf·自适应
beyond谚语2 天前
二、WPF——Style样式玩法(通过资源字典将Style独立,全局调用)
wpf
光辉岁月~3 天前
使用CalcBinding实现复杂逻辑绑定
wpf