自定义按钮的前景色(ControlPresenter)

Won*_*ane 5 wpf controls controltemplate

我试图在App.xaml中定义一个全局按钮样式,它主要按照我的预期工作.但是,我只是想不通如何让Foreground正常工作.无论我做什么,我都会得到默认TextBlock的样式(将颜色设置为白色).

    <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>
Run Code Online (Sandbox Code Playgroud)

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

谢谢,
wTs

Fre*_*lad 8

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

您可以TextBlock通过指定DataTemplatefor 来禁用为字符串创建的s 的隐式样式string.将以下内容添加到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>
Run Code Online (Sandbox Code Playgroud)