【问题标题】:Foreground Color of Custom Button (ControlPresenter)自定义按钮的前景色 (ControlPresenter)
【发布时间】:2011-03-16 18:08:16
【问题描述】:

我正在尝试在 App.xaml 中定义一个全局按钮样式,并且它主要按我的预期工作。但是,我只是无法弄清楚如何让前台正常工作。无论我做什么,我都会得到默认 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>

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

谢谢,
wTs

【问题讨论】:

    标签: wpf controls controltemplate


    【解决方案1】:

    就像 Markus Hütter 所说,问题可能是您定义了 TextBlock 的隐式样式,并且当 Button Content 设置为字符串时,将在您拥有 ContentPresenter 的位置创建 TextBlock在模板中。这个TextBlock 将采用隐式样式,这就是您遇到此问题的原因。

    您可以通过为string 指定DataTemplate 来禁用为字符串就地创建的TextBlocks 的隐式样式。将以下内容添加到 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>
    

    【讨论】:

      【解决方案2】:

      您似乎正在为文本块使用自定义样式(如果您说前景色为白色)我们称之为 StandardTextBlockStyle

      在外部网格内的按钮样式内。包括这个:

      <Grid x:Name="gridMainButton">
          <Grid.Resources>
              <Style TargetType="TextBlock" BasedOn="{StaticResource StandardTextBlockStyle}">
                  <Setter Property="Foreground" Value="Black"/>
              </Style>
          </Grid.Resources>
          <!-- ...-->
      </Grid>
      

      这应该覆盖默认的 TextBlock 样式。

      【讨论】:

        【解决方案3】:

        我有类似的问题,有一个全局 TextBlock 样式,我无法在需要自定义颜色/字体的其他控件中覆盖它。

        根据Fredrik HedbladPavlo Glazkov(来自here)的回复,以下解决方案对我有用:

        <DataTemplate DataType="{x:Type System:String}">
                <TextBlock Text="{Binding}">
                    <TextBlock.Resources>
                        <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
                            <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground),
                                FallbackValue={StaticResource Colors_ThemeForeground_SCBrush}, TargetNullValue={StaticResource Colors_ThemeForeground_SCBrush}}"/>
                        </Style>
                    </TextBlock.Resources>
                </TextBlock>
            </DataTemplate>
        

        谢谢, RDV

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-09-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-13
          相关资源
          最近更新 更多