WPF 模板 Template

在这里插入图片描述

运行效果

请添加图片描述

界面代码

<Window x:Class="Template.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:Template"
        mc:Ignorable="d"
        Title="MainWindow" Height="697" Width="800">
    <Window.Resources>

        <ControlTemplate x:Key="sdf" TargetType="{x:Type Button}">
            <Border x:Name="border"  Width="100"
                                CornerRadius="10" Background="Red"
                                BorderBrush="{TemplateBinding BorderBrush}" 
                                BorderThickness="{TemplateBinding BorderThickness}" 
                                SnapsToDevicePixels="true">
                <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                              Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsDefaulted" Value="true">
                    <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                </Trigger>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Background" TargetName="border" Value="Blue"/>
                    <Setter Property="BorderBrush" TargetName="border" Value="Blue"/>
                </Trigger>
                <EventTrigger RoutedEvent="MouseEnter">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetName="border"
                                                        Storyboard.TargetProperty="Background.Color" To="Pink"
                                                        Duration="0:0:0:2"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="MouseLeave">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetName="border"
                                                        Storyboard.TargetProperty="Background.Color" To="Green"
                                                        Duration="0:0:0:2"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>

            </ControlTemplate.Triggers>
        </ControlTemplate>

        <Style x:Key="FocusVisual">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle Margin="2" StrokeDashArray="1 2" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" SnapsToDevicePixels="true" StrokeThickness="1"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/>
        <SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/>
        <SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/>
        <SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/>
        <SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6"/>
        <SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B"/>
        <SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/>
        <SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/>
        <SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/>

        <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
            <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
            <Setter Property="Background" Value="{StaticResource Button.Static.Background}"/>
            <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Padding" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border x:Name="border"  Width="100"
                                CornerRadius="10" Background="Red"
                                BorderBrush="{TemplateBinding BorderBrush}" 
                                BorderThickness="{TemplateBinding BorderThickness}" 
                                SnapsToDevicePixels="true">
                            <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                              Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsDefaulted" Value="true">
                                <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" TargetName="border" Value="Blue"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="Blue"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/>
                                <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
                            </Trigger>
                            <EventTrigger RoutedEvent="MouseEnter">
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetName="border"
                                                        Storyboard.TargetProperty="Background.Color" To="Pink"
                                                        Duration="0:0:0:2"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                            <EventTrigger RoutedEvent="MouseLeave">
                                <BeginStoryboard>
                                    <Storyboard>
                                        <!--<DoubleAnimation Storyboard.TargetName="border"
                                                       Storyboard.TargetProperty="FrameworkElement.Width" To="1000"
                                                       Duration="0:0:0:2"/>-->
                                        <ColorAnimation Storyboard.TargetName="border"
                                                        Storyboard.TargetProperty="Background.Color" To="Green"
                                                        Duration="0:0:0:2"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>

                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>


        <DataTemplate x:Key="comTemplate">
            <StackPanel Orientation="Horizontal" Margin="5,0">
                <Border Width="10" Height="10" Background="{Binding Code}"/>
                <TextBlock Text="{Binding Code}" Margin="5,0"/>
            </StackPanel>
        </DataTemplate>

    </Window.Resources>
    <StackPanel>
        <Button Template="{StaticResource sdf}" Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="58" Width="225"/>
        <DataGrid Name="gd" AutoGenerateColumns="False" CanUserSortColumns="True"  CanUserAddRows="False">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding UserName}" Width="100" Header="学生姓名"/>
                <DataGridTextColumn Binding="{Binding ClassName}" Width="100" Header="班级名称"/>
                <DataGridTextColumn Binding="{Binding Address}" Width="200" Header="地址"/>
                <DataGridTemplateColumn Header="操作" Width="100" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Left">
                                <Button Content="编辑"/>
                                <Button Margin="8 0 0 0" Content="删除" />
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        
        <ComboBox Name="cob" Width="120" Height="30" ItemTemplate="{StaticResource comTemplate}"/>
        <ListBox Name="lib" Width="120" Height="100" Margin="5,0"  ItemTemplate="{StaticResource comTemplate}"/>

        <ItemsControl Name="ic">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>

            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Width="50" Height="50" Content="{Binding Code}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

    </StackPanel>
</Window>

后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Template
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {


        public MainWindow()
        {
            InitializeComponent();
            List<Student> students = new List<Student>();
            students.Add(new Student() { UserName = "小王", ClassName = "高二三班", Address = "广州市" });
            students.Add(new Student() { UserName = "小李", ClassName = "高三六班", Address = "清远市" });
            students.Add(new Student() { UserName = "小张", ClassName = "高一一班", Address = "深圳市" });
            students.Add(new Student() { UserName = "小黑", ClassName = "高一三班", Address = "赣州市" });

            List<UseColor> ColorList = new List<UseColor>();
            ColorList.Add(new UseColor() { Code = "#FF8C00" });
            ColorList.Add(new UseColor() { Code = "#FF7F50" });
            ColorList.Add(new UseColor() { Code = "#FF6EB4" });
            ColorList.Add(new UseColor() { Code = "#FF4500" });
            ColorList.Add(new UseColor() { Code = "#FF3030" });
            ColorList.Add(new UseColor() { Code = "#CD5B45" });

            cob.ItemsSource = ColorList;
            lib.ItemsSource = ColorList;
            gd.ItemsSource = students;

            List<UseColor> tests = new List<UseColor>();
            tests.Add(new UseColor() { Code = "1" });
            tests.Add(new UseColor() { Code = "2" });
            tests.Add(new UseColor() { Code = "3" });
            tests.Add(new UseColor() { Code = "4" });
            tests.Add(new UseColor() { Code = "6" });
            ic.ItemsSource = tests;
        }
    }
}

using Prism.Mvvm;


namespace Template
{
    public class Student :  BindableBase
    {
        private string userName=string.Empty;
        public string UserName { get => userName; set => SetProperty(ref userName, value); }

        private string className = string.Empty;
        public string ClassName { get => className; set => SetProperty(ref className, value); }

        private string address = string.Empty;
        public string Address { get => address; set => SetProperty(ref address, value); }

    }
}
using Prism.Mvvm;

namespace Template
{
    public class UseColor : BindableBase
    {
        private string code = string.Empty;

        public string Code { get => code; set => SetProperty(ref code, value); }
    }
}

备注

上述代码引入了Primer框架,主要作用是实现可观察对象。

觉得有用就请关注赞收藏吧!!!
工控之路,不迷茫

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: WPF(Windows Presentation Foundation)是一种用于构建Windows应用程序的技术,而UI template是一种在WPF中用于创建可重复使用的用户界面样式和布局的机制。 WPF的UI template可以理解为一种定义了界面元素外观和样式的模板。通过使用UI template,我们可以很方便地将相同的界面元素样式应用于多个控件,从而实现界面的一致性和可重用性。 使用UI template的好处之一是简化了界面开发工作。开发人员只需要定义一次UI模板,然后可以在需要的地方直接应用这个模板,而不需要重新编写和调整样式。这在多个界面元素需要拥有相同外观的情况下特别有用。 另一个好处是提高了界面的灵活性。通过修改UI template,我们可以轻松地调整控件的样式、布局和属性,而不会破坏应用程序的整体结构。这样可以节省大量的时间和工作量,并且可以使界面更容易维护。 在WPF中,UI template是由XAML语言定义的。通过使用XAML,开发人员可以以声明式的方式定义界面元素的外观和样式,而不需要编写繁琐的代码。这使得UI template的创建、修改和管理变得更加方便和直观。 总的来说,WPF的UI template是一种用于创建可重复使用的用户界面样式和布局的机制,它可以简化界面开发工作,提高界面的灵活性,并通过XAML语言实现了对界面的声明式定义。 ### 回答2: WPF(Windows Presentation Foundation)是微软推出的一种用于构建富客户端应用程序的技术。在WPF中,UI模板(UI Template)是一种定义了控件外观和行为的重要组成部分。 UI模板被用于定义控件在界面上的外观,比如控件的背景、边框、字体样式、布局等。通过使用UI模板,我们可以根据应用程序的需求,自定义控件的外观,使之符合我们期望的样式和风格。这种灵活性使得WPF应用程序可以呈现出各种独特的界面设计。 UI模板的创建主要分为两个步骤:定义模板和应用模板。首先,我们需要根据需要定义一个模板,可以使用XAML(eXtensible Application Markup Language)来描述模板的结构和样式。模板中可以包含多个可视元素和控件,并可以使用各种布局控件进行排列。其次,我们需要将定义好的模板应用到相应的控件上。在WPF中,通过将控件的Template属性设置为我们定义的模板,就可以使控件采用我们自定义的样式。 使用UI模板可以为应用程序带来很多好处。首先,它使得应用程序的界面设计能够与众不同,使其与其他应用程序区别开来,提供独特的用户体验。其次,UI模板的灵活性使得我们可以方便地修改和调整控件的外观,而无需改变控件的内部逻辑。这种分离了界面与逻辑的设计思想有助于增强应用程序的可维护性和可扩展性。 总之,WPF的UI模板是一种非常强大的工具,可以帮助开发者定制出各种独特且具有个性化的界面,从而提升应用程序的用户体验。 ### 回答3: WPF (Windows Presentation Foundation)是一种用于创建丰富而功能强大的Windows桌面应用程序的框架。在WPF中,UI模板是一种用于定义UI元素外观和行为的技术。 UI模板是一种XAML标记语言的结构,它描述了如何渲染和布局一个UI元素,从而决定了它的外观和行为。UI模板可以用于自定义各种控件,例如按钮、文本框、列表和窗体等。通过创建和应用UI模板开发者可以完全控制和定制应用程序的外观和交互方式。 WPF的UI模板由多个元素组成,其中包括容器元素、面板元素、控件元素和视觉元素等。容器元素用于包含和布局其他元素,面板元素用于定义元素的排列方式,控件元素用于添加交互和功能,视觉元素用于定义元素的外观。 开发者可以使用Visual Studio等工具创建和编辑UI模板。在模板中,可以通过修改元素的属性、样式和模板绑定来改变元素的外观和行为。还可以通过添加触发器和动画等功能来实现交互效果。 通过使用UI模板开发者可以根据自己的需求和设计要求来定制应用程序的外观。他们可以创建独特和个性化的用户界面,从而提供更好的用户体验。此外,UI模板还可以被多个控件共享,以实现统一的外观和风格。 总之,WPF的UI模板是一种强大的工具,可以让开发者自由定制和控制应用程序的外观和行为。通过使用UI模板,可以创建独特和个性化的用户界面,为用户提供更好的体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值