Silverlight 4/ XAML:如何在没有复制粘贴的情况下制作类似的按钮样式模板



我制作了自己的按钮模板,它应该是6种不同的颜色现在我为这些颜色中的一个做了一个样式。但是款式真的很大。我想做的是在其他颜色上也重复使用这种风格(除了颜色不同,其他样式都是一样的)

现在我在想。有没有一种方法可以创建这6个单独的样式,而不需要复制/粘贴6次,而是绑定这些颜色?

下面是用于说明的XAML代码:
<UserControl.Resources>
        <LinearGradientBrush x:Key="WitNormalBorder" EndPoint="1,1" StartPoint="0,0">
            <GradientStop Color="#FFFAFAFF" Offset="0.33"/>
            <GradientStop Color="#FFD4D4D7" Offset="1"/>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="WitNormalFill" EndPoint="1,1" StartPoint="0,0">
            <GradientStop Color="#FFD8D8DC" Offset="0"/>
            <GradientStop Color="White" Offset="0.66"/>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="WitOverBorder" EndPoint="1,1" StartPoint="0,0">
            <GradientStop Color="White" Offset="0.33"/>
            <GradientStop Color="#FFDCDCDE" Offset="1"/>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="WitOverFill" EndPoint="1,1" StartPoint="0,0">
            <GradientStop Color="#FFE6E6E9" Offset="0"/>
            <GradientStop Color="White" Offset="0.66"/>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="WitDownBorder" EndPoint="1,1" StartPoint="0,0">
            <GradientStop Color="#FFF0F0FF" Offset="0.33"/>
            <GradientStop Color="#FFD2D2D3" Offset="1"/>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="WitDownFill" EndPoint="1,1" StartPoint="0,0">
            <GradientStop Color="#FFDADADC" Offset="0"/>
            <GradientStop Color="#FFFAFAFD" Offset="0.66"/>
        </LinearGradientBrush>
        <Style x:Key="KleurButtonWit" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="Border" BorderThickness="0" Padding="5" CornerRadius="12" Background="{StaticResource WitNormalBorder}">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="Border" Storyboard.TargetProperty="(UIElement.Background)">
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource WitOverBorder}">
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="Background" Storyboard.TargetProperty="(UIElement.Background)">
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource WitOverFill}">
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(UIElement.Background)">
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource WitDownBorder}">
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="Background" Storyboard.TargetProperty="(UIElement.Background)">
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource WitDownFill}">
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled">
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="FocusStates">
                                    <VisualState x:Name="Focused">
                                    </VisualState>
                                    <VisualState x:Name="Unfocused"/>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Border x:Name="Background" BorderThickness="0" CornerRadius="8" Background="{StaticResource WitNormalFill}">
                                <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Border>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

你可以看到,如果我要复制这个6次,那将是一大堆代码…

谢谢,Matthy

您可以使用样式。BasedOn属性实现样式的继承。http://msdn.microsoft.com/en-us/library/system.windows.style.basedon.aspx这样可以避免重复常见元素。

如果你要同时使用这六种样式,你需要复制六次。我看没有别的办法。

如果你允许用户选择一个单一的样式,并且在给定的时间点只使用那个样式,这个问题可能也值得一看,阅读这个关于加载资源的问题

相关内容

  • 没有找到相关文章

最新更新