<Style x:Key="ButtonDefault" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<!--决定控件外观ControlTemplate,是控件Template属性的值-->
<ControlTemplate TargetType="{x:Type Button}">
<Grid Background="Yellow">
<!--可在此加入TemplateBinding
TemplateBinding 模板绑定扩展是用来把原对象中的属性和模板对象中的属性联系起来
-->
<!--<Ellipse Width="100" Height="50" HorizontalAlignment="Center" VerticalAlignment="Center">-->
<Ellipse Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" HorizontalAlignment="Center" VerticalAlignment="Center">
<Ellipse.Fill>
<RadialGradientBrush>
<GradientStop Color="Blue" Offset="1" />
<GradientStop Color="White" Offset="0" />
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
<Rectangle Width="80" Height="40">
<Rectangle.Fill >
<SolidColorBrush Color="Red">
</SolidColorBrush>
</Rectangle.Fill>
</Rectangle>
<!--<TextBlock FontSize="14" HorizontalAlignment="CenteR" VerticalAlignment="Center" Text="TextBlock" />-->
<!--去掉上面的内容 加入下面的内容后 button会显示它自身的内容
ContentPresenter 性能较好
下面3种写法任何一个均可-->
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
<!--<TextBlock FontSize="14" HorizontalAlignment="CenteR" VerticalAlignment="Center" Text="{TemplateBinding Content}" />-->
<!--<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="2">
<TextBlock Text="{Binding}" />
</Border>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>-->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<!--决定数据外观DataTemplate,是控件ContentTemplate属性的值-->
<DataTemplate>
<StackPanel>
<Image Source="../Pictures/On.png" Width="48" Height="48" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ButtonImage" TargetType="{x:Type Button}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel>
<TextBlock Text="Button" ></TextBlock>
<Image Source="../Pictures/On.png" Width="48" Height="48" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
wpf controltemplate contenttemplate
最新推荐文章于 2024-04-05 18:55:53 发布