【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>