【Silverlight】Templates的用法

        <Button Content="A Custom Button Template" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="174,120,0,0">
            <Button.Template>
                <ControlTemplate TargetType="Button" >
                    <Border BorderBrush="Orange" BorderThickness="3" CornerRadius="10"
 Background="Red">
                        <TextBlock Foreground="White" Text="{TemplateBinding Content}"></TextBlock>
                    </Border>
                </ControlTemplate>
            </Button.Template>
        </Button>

Reusing Control Templates

<UserControl.Resources>
 <ControlTemplate x: Key="ButtonTemplate" TargetType="Button" >
 <Border BorderBrush="Orange" BorderThickness="3" CornerRadius="10"
 Background="Red">
 <TextBlock Foreground="White" Text="A Custom Template"></TextBlock>
 </Border>
 </ControlTemplate>
</UserControl.Resources>

<Button Template="{StaticResource ButtonTemplate}" Content="A Templated Button"... >
</Button>

Setting Templates Through Styles

<Style x: Key="ButtonStyle" TargetType="Button">
 <Setter Property="Background" Value="Red"></Setter>
 <Setter Property="Template">
 <Setter.Value>
 <ControlTemplate TargetType="Button">
 <Border BorderBrush="Orange" BorderThickness="3" CornerRadius="10"
 Background="{TemplateBinding Background}">
 <ContentPresenter Margin="{TemplateBinding Padding}">
 </ContentPresenter>
 </Border>
 </ControlTemplate>
 </Setter.Value>
 </Setter>
</Style>


<Style x: Key="ButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Red"></Setter>
<Setter Property="Template" Value="{StaticResource ButtonTemplate}"></Setter>
</Style>

 

posted @ 2015-10-04 23:57  西楼月光  阅读(145)  评论(0编辑  收藏  举报