2011-03-16 29 views
4

我试图在App.xaml中定义一个全局按钮样式,它的大部分工作正如我所期望的那样。但是,我只是无法弄清楚如何让Foreground正常工作。无论我做什么,我都会得到默认TextBlock的样式(将颜色设置为白色)。自定义按钮的前景颜色(ControlPresenter)

<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

回答

7

像马库斯·特说,问题可能是,你必须定义一个TextBlock隐式风格而当Button Content被设置为一个字符串,一个TextBlock将被创建,你必须在模板中ContentPresenter。这TextBlock将拿起隐式风格,这就是为什么你得到这个问题。

通过为string指定DataTemplate,您可以禁用针对字符串创建的TextBlock的隐式样式。将以下内容添加到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> 
+0

+1不错,不知道你能做到这一点! – 2011-03-16 21:01:56

1

你似乎是使用文本块的自定义样式(如果你说前景色为白色)允许调用此StandardTextBlockStyle

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

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

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