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




相关推荐
Macbethad3 天前
使用WPF编写一个读取串口的程序
wpf
Macbethad4 天前
如何用WPF做工控设置界面
java·开发语言·wpf
csdn_aspnet4 天前
WPF 做一个简单的电子签名板(一)
c#·wpf
玖笙&4 天前
✨WPF编程进阶【7.2】:动画类型(附源码)
c++·c#·wpf·visual studio
·心猿意码·4 天前
WPF中TemplatePart机制详解
wpf
FuckPatience6 天前
WPF 使用UserControl / ContentControl显示子界面
wpf
wangnaisheng6 天前
【WPF】WrapPanel的用法
wpf
源之缘-OFD先行者6 天前
10 万雷达点迹零卡顿回放:WPF + Vortice.Direct2D 多线程渲染实战
wpf
猫林老师7 天前
Flutter for HarmonyOS开发指南(九):测试、调试与质量保障体系
flutter·wpf·harmonyos
LateFrames7 天前
做【秒开】的程序:WPF / WinForm / WinUI3 / Electron
electron·c#·wpf·winform·winui3·claude code