WPF控件模板、数据模板、容器样式选择器

news/2024/7/9 4:22:39

WPF控件模板

利用Tag来绑定控件模板内容

<!--模板定义-->
<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border x:Name="border" >
                            <UniformGrid Rows="2" Columns="1">
                            <TextBlock Text="{TemplateBinding Tag}" Background="Orange"/>
                            <ContentPresenter x:Name="contentPresenter"/>
                            </UniformGrid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
<!--使用-->
<Button Style="{DynamicResource ButtonStyle1}" Height="60" Width="100" Tag="内部text">
            ddd 
</Button>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-R03DnR76-1672023761028)(WPF控件模板.assets/image-20221209120204170.png)]

ItemsControl是基类,ListBox等都是继承自ItemsControl。ItemsControl里面有一个DataTemplate属性,可以定义数据模板

DataTemplate

<Window x:Class="WpfApp13.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:WpfApp13"
        mc:Ignorable="d"
        Title="MainWindow" Height="250" Width="400">
    <Window.Resources>
        <x:Array x:Key="datas" Type="local:Person">
            <local:Person Name="Tom1" Age="10" Top="10" Left="10"/>
            <local:Person Name="Tom2" Age="10" Top="30" Left="20"/>
            <local:Person Name="Tom3" Age="10" Top="50" Left="30"/>
            <local:Person Name="Tom4" Age="10" Top="70" Left="40"/>
            <local:Person Name="Tom5" Age="10" Top="90" Left="50"/>
            <local:Person Name="Tom5" Age="10" Top="110" Left="60"/>
            <local:Person Name="Tom5" Age="10" Top="130" Left="70"/>
        </x:Array>
    </Window.Resources>

    <StackPanel>
        <ListView ItemsSource="{StaticResource datas}" Height="218">
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas/>
                    <!--下面的ItemTemplate在这个容器中展示,默认是stackPanel-->
                    <!--但是并在不是直接在这个容器中展示,而是中间还有一层ContentPresenter-->
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>

			 <!--Canvas.Left等属性必须在这里设定而不能在下面的ItemTemplate-->
             <!--原因就是因为中间Canvas和ItemTemplate中间还有一层ContentPresenter-->
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Canvas.Left" Value="{Binding Left}"/>
                    <Setter Property="Canvas.Top"  Value="{Binding Top}"/>

                </Style>
            </ListView.ItemContainerStyle>

            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding Name}" Grid.Column="0"/>
                        <TextBlock Text="===" Grid.Column="1"/>
                        <TextBlock Text="{Binding Age}" Grid.Column="2" />
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackPanel>
</Window>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PtqyXoPs-1672023761028)(WPF控件模板.assets/image-20221209155128695.png)]

筛选数据

使用DataTrigger筛选数据

<Window x:Class="WpfApp12.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:WpfApp12"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="400">
    <Window.Resources>
        <x:Array Type="local:Person" x:Key="datas">
            <local:Person Name="Hello" Age="20" Gender="1"/>
            <local:Person Name="Zhaoxi" Age="21" Gender="2"/>
            <local:Person Name="Jovan" Age="22" Gender="1"/>
            <local:Person Name="Jovan" Age="22" Gender="1"/>
            <local:Person Name="Jovan" Age="18" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
        </x:Array>
    </Window.Resources>
    <Grid>
        <ListView ItemsSource="{StaticResource datas}" >
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>

            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Style.Triggers>
                        <!--在Style里面设置筛选-->
                        <!--使用DataTrigger筛选-->
                        <DataTrigger Binding="{Binding Age}" Value="20">
                            <Setter Property="Background" Value="Yellow"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ListView.ItemContainerStyle>

            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Name}"/>
                        <TextBlock Text="{Binding Age}" />
                        <TextBlock Text="{Binding Gender}"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>

在这里插入图片描述

数据模板选择器

但是使用DataTrigger功能比较少,如果要选择某个区间的数据,可以使用模板选择器

新建ListViewItemTemplateSelector类,该类继承自DataTemplateSelector

class ListViewItemTemplateSelector : DataTemplateSelector
{
    public DataTemplate NormalTemplate { get; set; }
    public DataTemplate AlarmTemplate { get; set; }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="item">每个控件子项所对应的数据子项</param>
    /// <param name="container"></param>
    /// <returns>当前的这个子项可使用的数据模板</returns>
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        var person = item as Person;
        if (person.Age > 21)
        {
            return AlarmTemplate;
        }
        return NormalTemplate;
    }
}

xml

<Window x:Class="WpfApp12.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:WpfApp12"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="400">
    <Window.Resources>
        <x:Array Type="local:Person" x:Key="datas">
            <local:Person Name="Hello" Age="20" Gender="1"/>
            <local:Person Name="Zhaoxi" Age="21" Gender="2"/>
            <local:Person Name="Jovan" Age="22" Gender="1"/>
            <local:Person Name="Jovan" Age="22" Gender="1"/>
            <local:Person Name="Jovan" Age="18" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
        </x:Array>
        
        <!--设置两个DataTemplae供选择器选择-->
        <DataTemplate x:Key="NormalTemp">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Name}" Background="Yellow"/>
                <TextBlock Text="{Binding Age}" Grid.Column="1"/>
                <TextBlock Text="{Binding Gender}" Grid.Column="2"/>
            </Grid>
        </DataTemplate>
        <DataTemplate x:Key="AlarmTemp">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Name}"/>
                <TextBlock Text="{Binding Age}" Grid.Column="1"/>
                <TextBlock Text="{Binding Gender}" Grid.Column="2"/>
            </Grid>
        </DataTemplate>


    </Window.Resources>
    <Grid>
        <ListView ItemsSource="{StaticResource datas}" >
            <ListView.ItemTemplateSelector>
                <!--设置两个属性-->
                <local:ListViewItemTemplateSelector NormalTemplate="{StaticResource NormalTemp}" AlarmTemplate="{StaticResource AlarmTemp}"/>
            </ListView.ItemTemplateSelector>
        </ListView>
    </Grid>
</Window>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BnyDjyNe-1670674384441)(模板选择器.assets/image-20221210122905622.png)]

容器样式选择器

也可以使用样式选择器

先创建一个ListViewStyleSelector类,继承自StyleSelector

class ListViewStyleSelector : StyleSelector
{
    public Style NormalStyle { get; set; }
    public Style AlarmStyle { get; set; }
    public override Style SelectStyle(object item, DependencyObject container)
    {
        var person = item as Person;
        if (person.Age > 21)
        {
            return AlarmStyle;
        }
        return NormalStyle;
    }
}

XAML

<Window x:Class="WpfApp12.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:WpfApp12"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="400">
    <Window.Resources>
        <x:Array Type="local:Person" x:Key="datas">
            <local:Person Name="Hello" Age="20" Gender="1"/>
            <local:Person Name="Zhaoxi" Age="21" Gender="2"/>
            <local:Person Name="Jovan" Age="22" Gender="1"/>
            <local:Person Name="Jovan" Age="22" Gender="1"/>
            <local:Person Name="Jovan" Age="18" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
            <local:Person Name="Jovan" Age="24" Gender="1"/>
        </x:Array>
        <!--设置两个Style供选择器选择-->
        <Style x:Key="NormalStyle" TargetType="ListViewItem">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate >
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="{Binding Name}"  Background="Yellow"/>
                            <TextBlock Text="{Binding Age}" Grid.Column="1"/>
                            <TextBlock Text="{Binding Gender}" Grid.Column="2"/>
                        </Grid>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="AlarmStyle" TargetType="ListViewItem">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate >
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="{Binding Name}"/>
                            <TextBlock Text="{Binding Age}" Grid.Column="1"/>
                            <TextBlock Text="{Binding Gender}" Grid.Column="2"/>
                        </Grid>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <ListView ItemsSource="{StaticResource datas}" >
            <ListView.ItemContainerStyleSelector>
                <local:ListViewStyleSelector NormalStyle="{StaticResource NormalStyle}" AlarmStyle="{StaticResource AlarmStyle}"/>
            </ListView.ItemContainerStyleSelector>
        </ListView>
    </Grid>
</Window>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lBEAjCA0-1670674384443)(模板选择器.assets/image-20221210124941393.png)]

总结:因为style中可以设置Template属性,又可以设置样式。所以在Templat标签中完成的内容都可以可以使用Style来代替。


http://lihuaxi.xjx100.cn/news/494909.html

相关文章

材料的几个性能指标

材料性能指标:弹性、断裂韧性、韧性、延展性title: 材料的几个性能指标 toc: true tags:- Abaques categories:- Abaques- 基础知识 abbrlink: 2bf41a83 date: 2022-06-17 00:38:21弹性—— 恢复原状的能力 材料的弹性,是指材料应力应变曲线中弹性阶段下方的面积。对于弹性阶…

第十三周内容总结

一、Q查询进阶操作 Q查询我们在上周初步学习的时候我们了解到他主要是用于实现查询过程中的一些逻辑运算符的使用,因为ORM中括号内的参数默认是and连接方式进行查询的。 而这里我们介绍的进阶操作,其实就是类似面向对象中的反射方法,实现的功能就是通过获取用户输入的字符串…

统计同构子字符串的数目

题目 给你一个字符串 s ,返回 s 中 同构子字符串 的数目。由于答案可能很大,只需返回对 109 + 7 取余 后的结果。 同构字符串 的定义为:如果一个字符串中的所有字符都相同,那么该字符串就是同构字符串。 子字符串 是字符串中的一个连续字符序列。示例 1: 输入:s = "…

【案例实践】基于Citespace和vosviewer文献计量学可视化SCI论文高效写作方法

【点击观看视频】基于Citespace和vosviewer文献计量学可视化SCI论文高效写作方法 文献计量学是指用数学和统计学的方法&#xff0c;定量地分析一切知识载体的交叉科学。它是集数学、统计学、文献学为一体&#xff0c;注重量化的综合性知识体系。特别是&#xff0c;信息可视化技…

Ubuntu系统中文乱码的解决办法

Ubuntu系统中文乱码的解决办法 文章目录Ubuntu系统中文乱码的解决办法1. 安装中文语言2. 安装语言设置的命令locale3. 安装中文的相关字体4. 修改语言的环境变量4.1 环境变量一4.2 设置二5. 正式配置语言后记最近在docker上pull下面的Ubuntu镜像运行后发现中文出现了乱码情况&a…

Windows和Mac系统实现本地部署WebPageTest工具

在项目开发或者测试的过程中&#xff0c;由于没有上线&#xff0c;我们在公网上无法访问我们的网站&#xff0c;但同时我们又需要查看浏览器性能&#xff0c;这样我们就需要在本地部署WebPageTest工具以协助进行性能测试 具体实现步骤&#xff1a; Windows系统&#xff1a; …

20.tornado项目结束(简单拓展一些技术栈)

本tornado项目从最开始的实现hello world到最后完整做成了一个不是很美观的但是&#xff01;各个功能完善的一个仿Instagram的网站项目。 相信大家如果认认真真跟着做下来&#xff0c;会学到很多&#xff01; 关于本项目也没什么需要多说的了&#xff0c;tornado已经早几年就几…

算法leetcode|26. 删除有序数组中的重复项(rust重拳出击)

文章目录26. 删除有序数组中的重复项&#xff1a;样例 1&#xff1a;样例 2&#xff1a;提示&#xff1a;分析&#xff1a;题解&#xff1a;rustgoccpythonjava26. 删除有序数组中的重复项&#xff1a; 给你一个 升序排列 的数组 nums &#xff0c;请你 原地 删除重复出现的元…