自定义按钮(ControlPresenter)的前景色 [英] Foreground Color of Custom Button (ControlPresenter)

查看:78
本文介绍了自定义按钮(ControlPresenter)的前景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在App.xaml中定义全局按钮样式,并且大多数情况下都可以按照我的期望进行工作.但是,我只是无法弄清楚如何使前景正常工作.无论我做什么,我都会得到默认TextBlock的样式(它将颜色设置为白色).

I am attempting to define a global button style in App.xaml, and it's mostly working as I expect. However, I just cannot figure out how to get the Foreground to work correctly. No matter what I do, I am getting the style of the default TextBlock (which sets the color to White).

    <Style TargetType="{x:Type Button}">
        <Setter Property="Margin" Value="3, 5" />
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="FocusVisualStyle" 
                Value="{StaticResource ButtonFocusVisual}" />
        <Setter Property="Foreground" Value="Red" />
        <Setter Property="Padding" Value="5" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid x:Name="gridMainButton"
                          RenderTransformOrigin="0.5, 0.5">
                        <Grid.RenderTransform>
                            <ScaleTransform x:Name="scaleTransform" 
                                            CenterX="0.5"
                                            CenterY="0.5" />
                        </Grid.RenderTransform>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates" >
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="MouseOver" />
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <DoubleAnimation
                                              Storyboard.TargetName="scaleTransform"
                                              Storyboard.TargetProperty="ScaleX"
                                              Duration="0"
                                              To="0.85" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>

                        <Ellipse x:Name="ellipse"
                                 HorizontalAlignment="Stretch"
                                 VerticalAlignment="Stretch"
                                 StrokeThickness="2"
                                 Stroke="{StaticResource standardBackground}"
                                 Fill="{StaticResource standardBackground}" />
                        <ContentPresenter HorizontalAlignment="Center"
                                          VerticalAlignment="Center"
                                          Margin="4, 8"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我想我可以将ContentPresenter更改为TextBlock,这对于该特定应用程序是可以的,但是我正在寻找更通用的解决方案.

I guess I could change the ContentPresenter to a TextBlock, which would be ok for this particular application, but I'm looking for a more generic solution.

谢谢,
wTs

Thanks,
wTs

推荐答案

就像MarkusHütter所说的那样,问题可能在于您为TextBlock定义了隐式样式,并且当Button Content设置为字符串时,在模板中有ContentPresenter的位置将创建一个TextBlock.此TextBlock将获取隐式样式,这就是为什么您遇到此问题的原因.

Like Markus Hütter said, the problem is probably that you have an implicit Style for a TextBlock defined and when the Button Content is set to a string, a TextBlock will be created where you have the ContentPresenter in the Template. This TextBlock will pick up the implicit Style and that's why you're getting this problem.

您可以通过为string指定一个DataTemplate来禁用为字符串创建的TextBlock的隐式样式.将以下内容添加到App.xaml

You can disable the implicit Style for a TextBlocks that is created in place for a string by specifying a DataTemplate for string. Add the following to App.xaml

<Application ...>
    <Application.Resources>
        <DataTemplate xmlns:sys="clr-namespace:System;assembly=mscorlib"
                      DataType="{x:Type sys:String}">
            <TextBlock Text="{Binding}">
                <TextBlock.Resources>
                    <Style TargetType="{x:Type TextBlock}"/>
                </TextBlock.Resources>
            </TextBlock>
        </DataTemplate>
        <!--...-->
    </Application.Resources>
</Application>

这篇关于自定义按钮(ControlPresenter)的前景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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