自定义控件的样式模板如
1 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 2 xmlns:control="clr-namespace:Resource.CommondControl" 3 xmlns:sys="clr-namespace:System;assembly=mscorlib" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 5 <Style x:Key="GlassButtonStyle" TargetType="{x:Type control:GlassButton}"> 6 <Setter Property="Template"> 7 <Setter.Value> 8 <ControlTemplate TargetType="{x:Type control:GlassButton}"> 9 <Grid Tag="{Binding ControlTag,RelativeSource={RelativeSource TemplatedParent}}"> 10 <Rectangle Stretch="Fill" StrokeThickness="2"> 11 <Rectangle.Fill> 12 <SolidColorBrush Color="{Binding ControlBrush,RelativeSource={RelativeSource TemplatedParent}}" Opacity="0.25882352941176473"/> 13 </Rectangle.Fill> 14 <Rectangle.Stroke> 15 <SolidColorBrush Color="{Binding ControlBrush,RelativeSource={RelativeSource TemplatedParent}}" Opacity="0.6"></SolidColorBrush> 16 </Rectangle.Stroke> 17 </Rectangle> 18 <Grid Margin="8"> 19 <Grid VerticalAlignment="Center" HorizontalAlignment="Center"> 20 <Image x:Name="PAR_Image" Source="{Binding ImageNormal,RelativeSource={RelativeSource TemplatedParent}}" 21 Height="{Binding ImageHeight,RelativeSource={RelativeSource TemplatedParent}}" 22 Width="{Binding ImageWidth,RelativeSource={RelativeSource TemplatedParent}}" 23 HorizontalAlignment="Center" VerticalAlignment="Center"> 24 <Image.Effect> 25 <DropShadowEffect ShadowDepth="0" BlurRadius="8" Color="#33BCE7F5"></DropShadowEffect> 26 </Image.Effect> 27 </Image> 28 </Grid> 29 <TextBlock x:Name="PAR_Text" Foreground="{Binding ForeBrush,RelativeSource={RelativeSource TemplatedParent}}" 30 FontSize="{Binding TextSize,RelativeSource={RelativeSource TemplatedParent}}" 31 FontFamily="{Binding TextFamily,RelativeSource={RelativeSource TemplatedParent}}" 32 Text="{Binding ContentText,RelativeSource={RelativeSource TemplatedParent}}" 33 VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="0,20,0,0" 34 IsHyphenationEnabled="True"> 35 <!--<TextBlock.Effect> 36 <DropShadowEffect ShadowDepth="0" BlurRadius="5" Color="#33BCE7F5"></DropShadowEffect> 37 </TextBlock.Effect>--> 38 </TextBlock> 39 </Grid> 40 </Grid> 41 </ControlTemplate> 42 </Setter.Value> 43 </Setter> 44 </Style> 45 </ResourceDictionary>
使用XamlWriter.Save(object obj)方法后,得到的字符串如下
1 <Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:rcc="clr-namespace:Resource.CommondControl;assembly=Resource" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Tag="实时态势"> 2 <rcc:GlassButton ImageNormal="/Resource;component/Images/TileLogo/6.png" ContentText="实时态势" FramBrush="{x:Null}" TextSize="18" TextFamily="STHeitiSC-Light" ForeBrush="#FFFEFEFE" ImageWidth="40" ImageHeight="40" ControlBrush="#FF58F31B" Width="247" Height="111"> 3 <rcc:GlassButton.Style> 4 <Style TargetType="rcc:GlassButton"> 5 <Style.Resources> 6 <ResourceDictionary/> 7 </Style.Resources> 8 <Setter Property="Control.Template"> 9 <Setter.Value> 10 <ControlTemplate TargetType="rcc:GlassButton"> 11 <Grid Tag="{x:Null}"> 12 <Rectangle Stretch="Fill" StrokeThickness="2"> 13 <Rectangle.Fill> 14 <SolidColorBrush Color="#00FFFFFF" Opacity="0.25882352941176473"/> 15 </Rectangle.Fill> 16 <Rectangle.Stroke> 17 <SolidColorBrush Color="#00FFFFFF" Opacity="0.6"/> 18 </Rectangle.Stroke> 19 </Rectangle> 20 <Grid Margin="8,8,8,8"> 21 <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> 22 <Image Source="{x:Null}" Name="PAR_Image" Width="Auto" Height="Auto" HorizontalAlignment="Center" VerticalAlignment="Center"> 23 <Image.Effect> 24 <DropShadowEffect ShadowDepth="0" Color="#33BCE7F5" BlurRadius="8"/> 25 </Image.Effect> 26 </Image> 27 </Grid> 28 <TextBlock FontFamily="微软雅黑" FontSize="12" Foreground="#FF000000" IsHyphenationEnabled="True" Name="PAR_Text" Margin="0,20,0,0" HorizontalAlignment="Center" VerticalAlignment="Bottom"/> 29 </Grid> 30 </Grid> 31 </ControlTemplate> 32 </Setter.Value> 33 </Setter> 34 </Style> 35 </rcc:GlassButton.Style> 36 </rcc:GlassButton> 37 </Grid>