WPF - 覆盖样式颜色,最佳实践 [英] WPF -- override style colors, best practice

查看:230
本文介绍了WPF - 覆盖样式颜色,最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GlassButton风格。的问题是,梯度建在风格和颜色中的梯度设置。现在,我有我喜欢的按钮,一个框架,但我需要不同的按钮是不同的颜色。有没有什么办法可以揭发,从款式颜色属性的一个或多个在我的按钮实现重写?下面风格:

如果没有的话,那有什么做这样的事情的最佳做法?我不希望有每种颜色创造出不同的风格,例如GlassButtonBlue,GlassButtonGreen,GlassButtonRed - 似乎有点小题大做......

 <风格X:键=GlassButton的TargetType ={X:类型按钮}>
    < setter属性=字号VALUE =42/>
    < setter属性=前景VALUE =白/>
    < setter属性=模板>
        < Setter.Value>
            <的ControlTemplate的TargetType ={X:类型按钮}>
                < BORDER X:NAME =ButtonBorder
CornerRadius =25,25,25,25
了borderThickness =4,4,4,4
背景=藏青
BorderBrush =#99FFFFFF
RenderTransformOrigin =0.5,0.5>
                    <网格和GT;
                        < Grid.RowDefinitions>
                            < RowDefinition高度=*/>
                            < RowDefinition高度=* 2.6/>
                        < /Grid.RowDefinitions>
                        &所述;边框Grid.Row =0CornerRadius =23,23,0,0>
                            < Border.Background>
                                <一个LinearGradientBrush StartPoint可以=0.5,0终点=0.5,1>
                                    <渐变停止X:名称=测试颜色=#08FFFFFF偏移量=0/>
                                    &所述;渐变停止颜色=#88FFFFFF偏移=1/>
                                < /一个LinearGradientBrush>
                            < /Border.Background>
                        < /边框>
                        <内容presenter X:NAME =ButtonContent presenter
                            VerticalAlignment =中心
                            Grid.RowSpan =2
                            的Horizo​​ntalAlignment =中心/>
                    < /网格和GT;
                < /边框>
                < ControlTemplate.Triggers>
                    <触发属性=是pressedVALUE =真>
                        < setter属性=的RenderTransform的TargetName =ButtonBorder>
                            < Setter.Value>
                                <&的TransformGroup GT;
                                    < ScaleTransform的ScaleX =0.9的scaleY =0.9/>
                                < /&的TransformGroup GT;
                            < /Setter.Value>
                        < /二传手>
                    < /触发>
                < /ControlTemplate.Triggers>
            < /控件模板>
        < /Setter.Value>
    < /二传手>
< /样式和GT;


解决方案

此情况下演示的 附加属性 ,它可以在你的 GlassButton 控件定义(输入的按钮在现实中),这样在你的XAML的值可以绑定到他们。一个简单的模板结合( {TemplateBinding MyAttachedProperty} )应该做的工作。

给一个去,让我知道,如果你需要在特定部分的任何帮助。

I have a style for a GlassButton. The problem is that the gradients are built in the style and the colors are set in the gradients. Now, I have a framework for a button that I like but I need different buttons to be different colors. Is there any way I can "expose" one or more of the color properties from the Style to override in my button implementations? Style below:

And if not, then what's the best practice for doing something like this? I don't want to have to create different styles for each color, e.g. "GlassButtonBlue, GlassButtonGreen, GlassButtonRed"--seems like overkill...

<Style x:Key="GlassButton" TargetType="{x:Type Button}">
    <Setter Property="FontSize" Value="42" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="ButtonBorder" 
		                  CornerRadius="25,25,25,25" 
		                  BorderThickness="4,4,4,4" 
		                  Background="CadetBlue"
		                  BorderBrush="#99FFFFFF"
		                  RenderTransformOrigin="0.5,0.5">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="2.6*"/>
                        </Grid.RowDefinitions>
                        <Border Grid.Row="0" CornerRadius="23,23,0,0">
                            <Border.Background>
                                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                    <GradientStop x:Name="test" Color="#08FFFFFF" Offset="0"/>
                                    <GradientStop Color="#88FFFFFF" Offset="1"/>
                                </LinearGradientBrush>
                            </Border.Background>
                        </Border>
                        <ContentPresenter x:Name="ButtonContentPresenter"   							
                            VerticalAlignment="Center"  
                            Grid.RowSpan="2" 
                            HorizontalAlignment="Center"/>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="RenderTransform" TargetName="ButtonBorder">
                            <Setter.Value>
                                <TransformGroup>
                                    <ScaleTransform ScaleX="0.9" ScaleY="0.9"/>
                                </TransformGroup>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

解决方案

This case demonstrates a good use for attached properties, which can be defined on your GlassButton control (of typed Button in actuality) so that the values in your XAML can bind to them. A simple template binding ({TemplateBinding MyAttachedProperty}) should do the job.

Give that a go and let me know if you need any help in specific parts.

这篇关于WPF - 覆盖样式颜色,最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆