2011-06-06 58 views
2

我在Buttons.xaml文件中定義了一個按鈕樣式。你能告訴我如何設置按鈕的前景屬性嗎?我需要使用漸變作爲前景屬性。代碼如下。現在我在按鈕標籤中設置漸變。在資源字典中設置按鈕的前景[文本]顏色

<Style x:Key="ButtonCancel" TargetType="{x:Type Button}"> 

     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        <ControlTemplate.Resources> 
         <Storyboard x:Key="Storyboard1"> 
          <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.Opacity)"> 
           <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/> 
           <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="0.68"/> 
          </DoubleAnimationUsingKeyFrames> 
          <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"> 
           <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/> 
           <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1.1"/> 
          </DoubleAnimationUsingKeyFrames> 
          <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"> 
           <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/> 
           <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1.1"/> 
          </DoubleAnimationUsingKeyFrames> 
          <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"> 
           <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/> 
           <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1.1"/> 
          </DoubleAnimationUsingKeyFrames> 
         </Storyboard> 
         <Storyboard x:Key="Storyboard_Copy1"> 
          <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.Opacity)"> 
           <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.68"/> 
           <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1"/> 
          </DoubleAnimationUsingKeyFrames> 
          <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"> 
           <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1.1"/> 
           <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1"/> 
          </DoubleAnimationUsingKeyFrames> 
          <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"> 
           <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1.1"/> 
           <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1"/> 
          </DoubleAnimationUsingKeyFrames> 
          <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"> 
           <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1.1"/> 
           <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1"/> 
          </DoubleAnimationUsingKeyFrames> 
         </Storyboard> 
        </ControlTemplate.Resources> 
        <Grid> 
         <Image x:Name="image" Source="../images/UnCategorized/Red_Oval_Button_With_Cross.png" Stretch="Fill" RenderTransformOrigin="0.5,0.5"> 
          <Image.RenderTransform> 
           <TransformGroup> 
            <ScaleTransform/> 
            <SkewTransform/> 
            <RotateTransform/> 
            <TranslateTransform/> 
           </TransformGroup> 
          </Image.RenderTransform> 
         </Image> 
         <ContentPresenter x:Name="contentPresenter" HorizontalAlignment="Center" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True" Content="CANCEL" RenderTransformOrigin="0.5,0.5"> 
          <ContentPresenter.RenderTransform> 
           <TransformGroup> 
            <ScaleTransform/> 
            <SkewTransform/> 
            <RotateTransform/> 
            <TranslateTransform/> 
           </TransformGroup> 
          </ContentPresenter.RenderTransform> 

         </ContentPresenter> 
        </Grid> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsFocused" Value="True"/> 
         <Trigger Property="IsDefaulted" Value="True"/> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Trigger.ExitActions> 
           <BeginStoryboard x:Name="Storyboard_Copy1_BeginStoryboard" Storyboard="{StaticResource Storyboard_Copy1}"/> 
          </Trigger.ExitActions> 
          <Trigger.EnterActions> 
           <BeginStoryboard Storyboard="{StaticResource Storyboard1}"/> 
          </Trigger.EnterActions> 
         </Trigger> 
         <Trigger Property="IsPressed" Value="True"/> 
         <Trigger Property="IsEnabled" Value="False"/> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 

      </Setter.Value> 
     </Setter> 
    </Style> 

<Button Style="{DynamicResource ButtonCancel}" Click="btnCancel_Clicked" FontFamily="Tw Cen MT" FontSize="18" FontWeight="Bold" Width="144" Name="btnCancel" 
       HorizontalAlignment="Right" Grid.Row="1" Grid.Column="1" Margin="0,73.5,0,55.5"> 
      <Button.OpacityMask> 
       <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
        <GradientStop Color="#9B000000" Offset="0"/> 
        <GradientStop Color="White" Offset="1"/> 
       </LinearGradientBrush> 
      </Button.OpacityMask> 

     </Button> 

回答

5

在風格?如果這樣使用setter:

<Style x:Key="ButtonCancel" TargetType="{x:Type Button}"> 
    <Setter Property="Foreground"> 
     <Setter.Value> 
      <!-- LinearGradientBrush here --> 
     </Setter.Value>  
    </Setter> 
    <!-- ........ --> 
</Style> 
+0

Thanks.It工作。你能告訴我如何將前景文字移動到左邊的幾個像素嗎? – logeeks 2011-06-06 11:20:44

+0

如果通過前景文本,你只是表示「取消」的文本,你應該將ContentPresenter的'Horizo​​ntalAlignment'設置爲'Left'。 – 2011-06-06 11:41:45

+0

非常感謝你H.B – logeeks 2011-06-07 06:47:57