zoukankan      html  css  js  c++  java
  • wpf之控件模板 安静点

     下面我们创建一个button控件

    <UserControl x:Class="MyWpf.ControlTemplateTest"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:MyWpf"
                 mc:Ignorable="d" 
                 d:DesignHeight="450" d:DesignWidth="800">
        <UserControl.Resources> 
            <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}"> 
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border  x:Name="border" CornerRadius="15" BorderBrush="{TemplateBinding BorderBrush}"
    BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> <ContentPresenter Content="测试" x:Name="contentPresenter" Focusable="False"
    HorizontalAlignment
    ="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True"

    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </UserControl.Resources> <Grid> <Button Width="100" Height="50" Style="{DynamicResource ButtonStyle1}"></Button> </Grid> </UserControl>

     界面:

     可以在Border标签中设置button的样式,当然也可以不在style标签中写也可以按照下面方式写:

    <UserControl x:Class="MyWpf.ControlTemplateTest"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:MyWpf"
                 mc:Ignorable="d" 
                 d:DesignHeight="450" d:DesignWidth="800">
        <UserControl.Resources> 
            <ControlTemplate x:Key="buttonTemplate" TargetType="Button">
                    <Border  x:Name="border" CornerRadius="15" BorderBrush="{TemplateBinding BorderBrush}" 
    BorderThickness
    ="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> <ContentPresenter Content="测试" x:Name="contentPresenter" Focusable="False"
    HorizontalAlignment
    ="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}"
    RecognizesAccessKey
    ="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
    VerticalAlignment
    ="{TemplateBinding VerticalContentAlignment}"/> </Border> </ControlTemplate> </UserControl.Resources> <Grid> <Button Width="100" Template="{StaticResource buttonTemplate}" Height="50" ></Button> </Grid> </UserControl>

    其它优秀博客:https://www.cnblogs.com/dingli/archive/2011/07/20/2112150.html,https://www.cnblogs.com/minhost/p/7600551.html

  • 相关阅读:
    Eclispe造成的tomcat占用端口 无法启动 强制终止进程 转载
    JavaScript在页面中的执行顺序(理解声明式函数与赋值式函数) 转载
    spket IDE插件更新地址
    SQL 语句外键 a foreign key constraint fails
    面试技能树 转载
    简单粗暴 每个servlet之前都插入一段代码解决 乱码问题
    记录一个因sqlmap导致的错误
    Java与数据库数据类型对应表
    乐观锁与悲观锁
    maven打的包中含源文件jar包
  • 原文地址:https://www.cnblogs.com/anjingdian/p/15526584.html
Copyright © 2011-2022 走看看