WPF样式:Styled BasedOn使用全局颜色

时间:2015-10-29 14:42:03

标签: wpf

我有一个改变按钮外观的样式,但是这个按钮可以是不同的颜色,但我这样做是通过使用basedOn属性并引用主按钮样式但更改背景颜色。见下文:

<!--- Small Button Colours -->
<Style BasedOn="{StaticResource ButtonIcoSmall}" TargetType="Button" x:Key="BtnSmGreen">
    <Style.Resources>
        <SolidColorBrush Color="#FF5E8108" x:Key="colour1" />
        <SolidColorBrush Color="#FF5E8107" x:Key="colour2" />
        <SolidColorBrush Color="#FFA1D423" x:Key="colour3" />
    </Style.Resources>
</Style>

但是,我没有将这些颜色移动到样式中并转换为全局SolidBrushColour标记,因此我可以在任何样式中使用这些颜色而无需再次键入颜色,并使其更容易全局更改。

<!-- Global Colour Scheme -->

<!-- Green -->
<SolidColorBrush Color="#FF8AB71C" x:Key="NorGreen" />
<SolidColorBrush Color="#FF5E8107" x:Key="DarGreen" />
<SolidColorBrush Color="#FFA1D423" x:Key="LigGreen" />

<!-- Grey -->
<SolidColorBrush Color="#FF434953" x:Key="NorGrey" />
<SolidColorBrush Color="#FF22252b" x:Key="DarGrey" />
<SolidColorBrush Color="#FF58616E" x:Key="LigGrey" />

<!-- Blue -->
<SolidColorBrush Color="#FF29A1D5" x:Key="NorBlue" />
<SolidColorBrush Color="#FF1F7297" x:Key="DarBlue" />
<SolidColorBrush Color="#FF36BBF5" x:Key="LigBlue" />

<!-- Red -->
<SolidColorBrush Color="#FFEE4E10" x:Key="NorRed" />
<SolidColorBrush Color="#FFAA3C12" x:Key="DarRed" />
<SolidColorBrush Color="#FFFF6F37" x:Key="LigRed" />
<!-- Global Colour Scheme END -->

现在,如何在我的BasedOn样式中引用全局颜色,我已经尝试Colour="{StaticResource NorGreen}这不起作用,我假设由于数据类型。下面是使用这些颜色的样式,你想看看它:

<Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <WrapPanel>
                        <Border Grid.Column="0"
                                    CornerRadius="5 0 0 5" 
                                    BorderThickness="0"
                                    Background="{DynamicResource colour1}"
                                    Name="buttonIcon">
                            <WrapPanel>
                                <ContentPresenter Grid.Column="0" 
                                                      HorizontalAlignment="Center" 
                                                      VerticalAlignment="Center" 
                                                      Margin="{TemplateBinding Margin}" />
                            </WrapPanel>
                        </Border>
                        <Border Grid.Column="0"
                                    CornerRadius="0 5 5 0" 
                                    BorderThickness="0"
                                    Background="{DynamicResource colour2}"
                                    Name="buttonText" >
                            <TextBlock Text="{TemplateBinding Tag}" 
                                                       FontSize="13"
                                                       Foreground="White"
                                                       Padding="{TemplateBinding Padding}" />
                        </Border>
                    </WrapPanel>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="{DynamicResource colour1}" TargetName="buttonText" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" Value="{DynamicResource colour3}" TargetName="buttonText" />
                            <Setter Property="Background" Value="{DynamicResource colour3}" TargetName="buttonIcon" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

1 个答案:

答案 0 :(得分:0)

管理找到答案,实际上相当简单:/

Color="{Binding Source={StaticResource NorGreen}, Path=Color}"