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




相关推荐
The Sheep 20233 小时前
WPF自定义路由事件
大数据·hadoop·wpf
阳光雨滴3 小时前
使用wpf用户控件编程落石效果动画
c++·wpf
wuty0073 小时前
WPF 调用 ChangeWindowMessageFilterEx 修改指定窗口 (UIPI) 消息筛选器的用户界面特权隔离
wpf·sendmessage·changewindowmessagefilterex·uip·消息筛选器的用户界面特权隔离·window message
攻城狮CSU10 小时前
WPF中核心接口 INotifyPropertyChanged
wpf
c#上位机10 小时前
wpf之Interaction.Triggers
c#·wpf
是木子啦14 小时前
wpf passwordbox控件 光标移到最后
c#·wpf
The Sheep 202314 小时前
wpf 命令理解
wpf
布伦鸽14 小时前
C# WPF DataGrid使用Observable<Observable<object>类型作为数据源
开发语言·c#·wpf
分布式存储与RustFS1 天前
告别复杂配置:用Milvus、RustFS和Vibe Coding,60分钟DIY专属Chatbot
wpf·文件系统·milvus·对象存储·minio·rustfs·vibe
攻城狮CSU2 天前
WPF 绑定机制实现原理
wpf