WPF - 自定义可重用按钮不显示任何文本

时间:2013-03-26 17:49:53

标签: wpf xaml styles

这是我目前用来设置一个可重用的简单后退按钮的代码,问题是我还需要另一个具有相同属性的按钮,但可以根据屏幕指定文本将被使用。

为了显示效果和文字,我需要添加什么? (我尝试添加文本只是让按钮完全消失。)

<UserControl x:Class="SantaBarbara.Resources.BackButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             >
    <Button Width="116" Height="119" Canvas.Top="57" BorderThickness="0" BorderBrush="Transparent">
        <Button.Style>            
            <Style TargetType="{x:Type Button}">
                <Style.Resources>
                    <SolidColorBrush x:Key="BackgroundBrush" Color="#990050" Opacity="0.50" />                    
                </Style.Resources>
                <Setter Property="OverridesDefaultStyle" Value="True"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">

                            <ControlTemplate.Resources>
                                <Storyboard x:Key="MouseOverAnimation">
                                    <DoubleAnimation Duration="0:0:0.5" To="0.9" Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Opacity)" Storyboard.TargetName="Border" />
                                </Storyboard>
                                <Storyboard x:Key="MouseOutAnimation">
                                    <DoubleAnimation Duration="0:0:0.5" To="0.5" Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Opacity)" Storyboard.TargetName="Border" />
                                </Storyboard>
                            </ControlTemplate.Resources>

                            <Grid>
                                <Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" >
                                    <ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}"  Content="{TemplateBinding Content}" HorizontalAlignment="Center" Margin="{TemplateBinding Padding}"/>
                                </Border>
                            </Grid>

                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Trigger.EnterActions>
                                        <BeginStoryboard Storyboard="{StaticResource MouseOverAnimation}" />
                                    </Trigger.EnterActions>
                                    <Trigger.ExitActions>
                                        <BeginStoryboard Storyboard="{StaticResource MouseOutAnimation}" />
                                    </Trigger.ExitActions>
                                </Trigger>
                            </ControlTemplate.Triggers>

                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Button.Style>        
    </Button>
</UserControl>

1 个答案:

答案 0 :(得分:1)

你需要一点点补充。在Button

中设置Background的默认Style

您有Brush作为资源,需要在默认样式设置器中设置它。

类似的东西:

<Style TargetType="{x:Type Button}">
  <Style.Resources>
    <SolidColorBrush x:Key="BackgroundBrush"
                     Opacity="0.50"
                     Color="#990050" />
  </Style.Resources>
  <Setter Property="OverridesDefaultStyle"
          Value="True" />
  <Setter Property="Background"
          Value="{StaticResource BackgroundBrush}" />
  ...

然后,您可以提取Style,指定x:Key并将其应用于您想要的按钮。刚尝试过,它可以与动画一起使用