WPF 控件模板

WPF 控件模板

示例一:
下面以Button控件为例:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" x:Class="WPF0309.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ControlTemplate x:Key="mybtnTem" TargetType="{x:Type Button}">
            <Border BorderThickness="2" BorderBrush="Green" CornerRadius="10">
                <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                    <TextBlock Text="❤"></TextBlock>
                    <ContentPresenter></ContentPresenter>
                </StackPanel>
            </Border>
        </ControlTemplate>
    </Window.Resources>
    <Grid>
        <Button Height="50" Width="100" Content="OK" Template="{StaticResource mybtnTem}"/>
    </Grid>
</Window>

效果:
在这里插入图片描述
示例二:

<Window x:Class="WPF0418A.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ControlTemplate x:Key="btnTemplate" TargetType="{x:Type Button}">
            <Border BorderThickness="2" BorderBrush="Green" CornerRadius="10" Background="SkyBlue" Name="border">
                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"></ContentPresenter>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="true" >
                    <Setter TargetName="border" Property="Background" Value="Red"></Setter>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Button Content="GOOD" Template="{StaticResource btnTemplate}"/>
        </StackPanel>
    </Grid>
</Window>

效果:(按钮背景为浅蓝色,当鼠标放进去时变成红色。)
在这里插入图片描述
示例三:添加一个按钮的控件模板
1.新建一个资源字典文件:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:Mainform">
    <ControlTemplate x:Key="btnTemplate" TargetType="{x:Type Button}">
        <Border  Name="border">
            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"></ContentPresenter>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="true" >
                <Setter TargetName="border" Property="Background" Value="#1d953f"></Setter>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="false" >
                <Setter TargetName="border" Property="Background" Value="Green"></Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</ResourceDictionary>

2.在App.xaml中添加对其的引用:

 <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="DictionaryStyle.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

3.使用方法:

<Button Content="计算" Width="150" Template="{StaticResource btnTemplate}" Foreground="White" FontWeight="Bold"></Button>

效果:鼠标进入和离开按钮,按钮背景色改变

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值