WPF模板

先来个模板:

<ControlTemplate x:Key="button" TargetType="Button">

            <Border CornerRadius="4" BorderThickness="3">

                <Border.BorderBrush>

                    <LinearGradientBrush EndPoint="0,1">

                        <GradientStop Offset="0" Color="#FF95EB09"/>

                        <GradientStop Offset="1" Color="Yellow"/>

                    </LinearGradientBrush>

                </Border.BorderBrush>

                <Border.Background>

                    <LinearGradientBrush EndPoint="0,1">

                        <GradientStop Offset="0" Color="Yellow"/>

                        <GradientStop Offset="1" Color="#FF95EB09"/>

                    </LinearGradientBrush>

                </Border.Background>

                <ContentPresenter  HorizontalAlignment="Center"

                                   VerticalAlignment="Center"/>

            </Border>

            

        </ControlTemplate>

注:边框与背景的渐变色相反可以显得有质感,

效果如:

1.模板绑定

  我们希望能够添加一些参数到模板中,或者利用来自模板控件中的属性更进一步地自定义控件,

所以我们可以在模板控件上绑定属性,让控件使用者能够调整控件上的属性并自定义模板,代码如:

 <ControlTemplate x:Key="button" TargetType="{x:Type Button}">

            <Border CornerRadius="4" BorderThickness="{TemplateBinding Property=BorderThickness}"

                    BorderBrush="{TemplateBinding Property=BorderBrush}"

                    Background="{TemplateBinding Property=Background}"

                    >

                <ContentPresenter/>



            </Border>

        </ControlTemplate>

  这段代码把来自Border的三个属性绑定到了模板化的Button控件上同样的属性上。这样做以后,只需简单

的设置Button上的属性,就可以了创建下图的按钮:

 <ControlTemplate x:Key="button" TargetType="{x:Type Button}">

            <Border CornerRadius="4" BorderThickness="{TemplateBinding Property=BorderThickness}"

                    BorderBrush="{TemplateBinding Property=BorderBrush}"

                    Background="{TemplateBinding Property=Background}"

                    >

                <ContentPresenter/>



            </Border>

        </ControlTemplate>

 

你可能感兴趣的:(WPF)