<Style x:Key="RoundButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="RootElement">
<Rectangle Width="57" Height="22" RadiusX="6" RadiusY="8"> <!--设置圆角-->
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> <!--设置渐变效果可以调整坐标-->
<GradientStop Color="#ffffff" Offset="0"/><!--上边-->
<GradientStop Color="#039EF8" Offset="0.5"/><!--中间-->
<GradientStop Color="#10DFFE" Offset="0.961"/>
</LinearGradientBrush>
</Rectangle.Fill>
<Rectangle.Stroke> <!--边框-->
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#00DEF7" Offset="0" />
<GradientStop Color="#FFFFFF" Offset="1" />
</LinearGradientBrush>
</Rectangle.Stroke>
</Rectangle>
<ContentControl x:Name="contentControl" Width="auto" FontSize="13" Height="auto" VerticalAlignment="Center" Foreground="White" HorizontalAlignment="Center"
Content="{TemplateBinding Content}">
</ContentControl>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
显示的文字需要用这种现实绑定不然后台改不了显示的内容了
<ContentControl x:Name="contentControl" Width="auto" FontSize="13" Height="auto" VerticalAlignment="Center" Foreground="White" HorizontalAlignment="Center"
Content="{TemplateBinding Content}">
</ContentControl>
后台引用自定义样式
那么我们如何通过后台编码将这一样式应用到控件上呢?其实很简单,只需要一行代码即可:
textBlock.Style = Application.Current.Resources“[RoundButton"] as Style;
上面的样式是定义在App.xml中的全局样式,如果我们需要应用定义在页面中的样式,只需要稍作调整,代码如下:
textBlock.Style = Resources["MyTextBoxStyle"] as Style;
http://silverlightchina.net/html/study/WPF/2012/0927/19210.html