WPF按钮样式与textBlock contentTemplate,如何更改textblock前景色故事板

时间:2014-11-15 12:46:34

标签: wpf xaml button styles contenttemplate

我有以下按钮样式。我的问题是,如何在未启用按钮时更改文本块的文本颜色?我尝试通过故事板设置“ButtonTextBlock”值,但似乎无法访问它。感谢。

<Style TargetType="{x:Type Button}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Viewbox>
                    <!-- TODO change this textblocks colour depending on IsEnabled -->
                    <TextBlock x:Name="ButtonTextBlock" HorizontalAlignment="Center" VerticalAlignment="Stretch" Margin="0,2,0,2">
                        <ContentPresenter Content="{TemplateBinding Content}" >
                        </ContentPresenter>
                    </TextBlock>
                </Viewbox>
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="BorderThickness" Value="1"></Setter>
    <Setter Property="BorderBrush" Value="{StaticResource ButtonBorderBrush}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0">
                    <Border.Background>
                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
                            <GradientStop x:Name="BackgroundGradientStop1" Offset="0" Color="{StaticResource AlbumScrollButtonNormal1}" />
                            <GradientStop x:Name="BackgroundGradientStop2" Offset="1" Color="{StaticResource AlbumScrollButtonNormal2}" />
                        </LinearGradientBrush>
                    </Border.Background>
                    <ContentPresenter Margin="{TemplateBinding Padding}" 
                                      Content="{TemplateBinding Content}"
                                      ContentTemplate="{TemplateBinding ContentTemplate}"
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>          

                <ControlTemplate.Triggers>
                    <!-- Button Disabled -->
                    <Trigger Property="IsEnabled" Value="False">
                        <!-- Disabled -->
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard x:Name="ButtonDisabled">
                                    <ColorAnimation Storyboard.TargetName="BackgroundGradientStop1"
                                                    Storyboard.TargetProperty="Color"
                                                    To="{StaticResource AlbumScrollButtonDisabled1}" 
                                                    Duration="0" />
                                    <ColorAnimation Storyboard.TargetName="BackgroundGradientStop2"
                                                    Storyboard.TargetProperty="Color"
                                                    To="{StaticResource AlbumScrollButtonDisabled2}"
                                                    Duration="0" />
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions> 

                        <!-- Enabled -->
                        <Trigger.ExitActions>
                            <BeginStoryboard>
                                <Storyboard x:Name="ButtonEnabled">
                                    <ColorAnimation Storyboard.TargetName="BackgroundGradientStop1"
                                                    Storyboard.TargetProperty="Color"
                                                    To="{StaticResource AlbumScrollButtonNormal1}"
                                                    Duration="0" />
                                    <ColorAnimation Storyboard.TargetName="BackgroundGradientStop2"
                                                    Storyboard.TargetProperty="Color"
                                                    To="{StaticResource AlbumScrollButtonNormal2}"
                                                    Duration="0" />
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.ExitActions>
                    </Trigger>

                    <!-- Mouse Over and Mouse leave -->
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsEnabled" Value="True" />
                            <Condition Property="IsMouseOver" Value="True" />
                        </MultiTrigger.Conditions>

                        <!-- Mouse Over -->
                        <MultiTrigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard x:Name="MouseOverAnimation">
                                    <ColorAnimation Storyboard.TargetName="BackgroundGradientStop1"
                                                    Storyboard.TargetProperty="Color"
                                                    To="{StaticResource AlbumScrollButtonMouseOver1}"
                                                    Duration="0:0:0:0.1" />
                                    <ColorAnimation Storyboard.TargetName="BackgroundGradientStop2"
                                                    Storyboard.TargetProperty="Color"
                                                    To="{StaticResource AlbumScrollButtonMouseOver2}"
                                                    Duration="0:0:0:0.1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </MultiTrigger.EnterActions>

                        <!-- Mouse Leave -->
                        <MultiTrigger.ExitActions>
                            <BeginStoryboard>
                                <Storyboard x:Name="MouseLeaveAnimation" FillBehavior="Stop">
                                    <ColorAnimation Storyboard.TargetName="BackgroundGradientStop1"
                                                    Storyboard.TargetProperty="Color"
                                                    To="{StaticResource AlbumScrollButtonNormal1}"
                                                    Duration="0:0:0:0.1" />
                                    <ColorAnimation Storyboard.TargetName="BackgroundGradientStop2"
                                                    Storyboard.TargetProperty="Color"
                                                    To="{StaticResource AlbumScrollButtonNormal2}"
                                                    Duration="0:0:0:0.1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </MultiTrigger.ExitActions>
                    </MultiTrigger>  

                    <!-- Mouse Press -->
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsEnabled" Value="True" />
                            <Condition Property="IsPressed" Value="True" />
                        </MultiTrigger.Conditions>
                        <!-- Mouse Down -->
                        <MultiTrigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard x:Name="MouseDownAnimation" >
                                    <ColorAnimation Storyboard.TargetName="BackgroundGradientStop1"
                                                    Storyboard.TargetProperty="Color"
                                                    To="{StaticResource AlbumScrollButtonPressed1}"
                                                    Duration="0:0:0:0.1" />
                                    <ColorAnimation Storyboard.TargetName="BackgroundGradientStop2"
                                                    Storyboard.TargetProperty="Color"
                                                    To="{StaticResource AlbumScrollButtonPressed2}"
                                                    Duration="0:0:0:0.1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </MultiTrigger.EnterActions>

                        <!-- Mouse Up -->
                        <MultiTrigger.ExitActions>
                            <BeginStoryboard>
                                <Storyboard x:Name="MouseUpAnimation" FillBehavior="Stop">
                                    <ColorAnimation Storyboard.TargetName="BackgroundGradientStop1"
                                                    Storyboard.TargetProperty="Color"
                                                    To="{StaticResource AlbumScrollButtonMouseOver1}"
                                                    Duration="0:0:0:0.1" />
                                    <ColorAnimation Storyboard.TargetName="BackgroundGradientStop2"
                                                    Storyboard.TargetProperty="Color"
                                                    To="{StaticResource AlbumScrollButtonMouseOver2}"
                                                    Duration="0:0:0:0.1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </MultiTrigger.ExitActions>
                    </MultiTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:1)

您可以在Foreground.Color时为Button的{​​{1}}设置动画。此外,IsEnabled=False中不需要ContentTemplate。您可以将Style移至ViewBox并移除ControlTemplate

ContentTemplate