按钮中的TextBlock内容与自定义样式不可见?

时间:2013-02-08 02:04:08

标签: .net wpf

我为我的按钮定义了自定义样式。我想在按钮内放一个TextBlock,以便我可以利用文本换行,但如果我对内容使用TextBlock,则文本不会显示。

设计师看起来像这样:

Invisible text

Button定义为:

<Button HorizontalAlignment="Center"
        VerticalAlignment="Center">
  <Button.Content>
    <TextBlock Text="Lorem ipsum dolor sit amet, consectetur adipisicing elit."
               Width="150"
               TextWrapping="Wrap"
               FontSize="20" />
  </Button.Content>
</Button>

Style定义为:

<Style TargetType="Button">
  <Style.Setters>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="Button">
          <ControlTemplate.Resources>
            <Storyboard x:Key="ShowShine">
              <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                             Storyboard.TargetName="Shine"
                                             Storyboard.TargetProperty="(UIElement.Opacity)">
                <SplineDoubleKeyFrame KeyTime="0"
                                      Value="1" />
              </DoubleAnimationUsingKeyFrames>
            </Storyboard>
            <Storyboard x:Key="HideShine">
              <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                             Storyboard.TargetName="Shine"
                                             Storyboard.TargetProperty="(UIElement.Opacity)">
                <SplineDoubleKeyFrame KeyTime="0"
                                      Value="0" />
              </DoubleAnimationUsingKeyFrames>
            </Storyboard>
            <Storyboard x:Key="ShowMatte">
              <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                             Storyboard.TargetName="Matte"
                                             Storyboard.TargetProperty="(UIElement.Opacity)">
                <SplineDoubleKeyFrame KeyTime="0"
                                      Value="1" />
              </DoubleAnimationUsingKeyFrames>
            </Storyboard>
            <Storyboard x:Key="HideMatte">
              <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                             Storyboard.TargetName="Matte"
                                             Storyboard.TargetProperty="(UIElement.Opacity)">
                <SplineDoubleKeyFrame KeyTime="0"
                                      Value="0" />
              </DoubleAnimationUsingKeyFrames>
            </Storyboard>
          </ControlTemplate.Resources>
          <Grid>
            <Border Background="#FFEEEEEE"
                    BorderBrush="#00FFFFFF"
                    Padding="10 5"
                    BorderThickness="0"
                    Margin="0 0 0 0">
              <ContentPresenter VerticalAlignment="Center"
                                Grid.RowSpan="2"
                                Margin="{TemplateBinding Padding}"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
            </Border>
            <Border Background="#FF51a6ee"
                    BorderBrush="#FF51a6ee"
                    Padding="10 5"
                    Margin="0 0 0 0"
                    Opacity="0"
                    BorderThickness="0"
                    Name="Shine">
              <ContentPresenter VerticalAlignment="Center"
                                Grid.RowSpan="2"
                                Margin="{TemplateBinding Padding}"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
            </Border>
            <Border Background="#0b86ef"
                    BorderBrush="#0b86ef"
                    Padding="10 5"
                    Margin="0 0 0 0"
                    Opacity="0"
                    BorderThickness="0"
                    Name="Pressed">
              <ContentPresenter VerticalAlignment="Center"
                                Grid.RowSpan="2"
                                Margin="{TemplateBinding Padding}"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
            </Border>
            <Border Background="#FFAAAAAA"
                    BorderBrush="#00FFFFFF"
                    Padding="10 5"
                    BorderThickness="0"
                    Margin="0 0 0 0"
                    Opacity="0"
                    Name="Matte">
              <ContentPresenter VerticalAlignment="Center"
                                Grid.RowSpan="2"
                                Margin="{TemplateBinding Padding}"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
            </Border>
          </Grid>
          <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver"
                     Value="True">
              <Trigger.ExitActions>
                <BeginStoryboard Storyboard="{StaticResource HideShine}" />
              </Trigger.ExitActions>
              <Trigger.EnterActions>
                <BeginStoryboard Storyboard="{StaticResource ShowShine}" />
              </Trigger.EnterActions>
            </Trigger>
            <Trigger Property="IsPressed"
                     Value="True">
              <Setter Property="Opacity"
                      TargetName="Pressed"
                      Value="1" />
            </Trigger>
            <Trigger Property="IsEnabled"
                     Value="False">
              <Trigger.ExitActions>
                <BeginStoryboard Storyboard="{StaticResource HideMatte}" />
              </Trigger.ExitActions>
              <Trigger.EnterActions>
                <BeginStoryboard Storyboard="{StaticResource ShowMatte}" />
              </Trigger.EnterActions>
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style.Setters>
</Style>

TextBlock显然是“那里”,但它没有出现 - 无论前景颜色选择如何。知道如何让TextBlock出现在按钮中吗?

修改 ContentPresenters彼此重叠。以下样式按预期工作:

  <Style TargetType="Button">
    <Style.Setters>
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="Button">
            <ControlTemplate.Resources>
              <Storyboard x:Key="ShowShine">
                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                               Storyboard.TargetName="Shine"
                                               Storyboard.TargetProperty="(UIElement.Opacity)">
                  <SplineDoubleKeyFrame KeyTime="0"
                                        Value="1" />
                </DoubleAnimationUsingKeyFrames>
              </Storyboard>
              <Storyboard x:Key="HideShine">
                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                               Storyboard.TargetName="Shine"
                                               Storyboard.TargetProperty="(UIElement.Opacity)">
                  <SplineDoubleKeyFrame KeyTime="0"
                                        Value="0" />
                </DoubleAnimationUsingKeyFrames>
              </Storyboard>
              <Storyboard x:Key="ShowMatte">
                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                               Storyboard.TargetName="Matte"
                                               Storyboard.TargetProperty="(UIElement.Opacity)">
                  <SplineDoubleKeyFrame KeyTime="0"
                                        Value="1" />
                </DoubleAnimationUsingKeyFrames>
              </Storyboard>
              <Storyboard x:Key="HideMatte">
                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                               Storyboard.TargetName="Matte"
                                               Storyboard.TargetProperty="(UIElement.Opacity)">
                  <SplineDoubleKeyFrame KeyTime="0"
                                        Value="0" />
                </DoubleAnimationUsingKeyFrames>
              </Storyboard>
            </ControlTemplate.Resources>
            <Grid>
              <Border Background="#FFEEEEEE"
                      BorderBrush="#00FFFFFF"
                      Padding="10 5"
                      BorderThickness="0" />
              <Border Background="#FF51a6ee"
                      BorderBrush="#FF51a6ee"
                      Padding="10 5"
                      Opacity="0"
                      BorderThickness="0"
                      Name="Shine" />
              <Border Background="#0b86ef"
                      BorderBrush="#0b86ef"
                      Padding="10 5"
                      Opacity="0"
                      BorderThickness="0"
                      Name="Pressed" />
              <Border Padding="10 5">
                <ContentPresenter VerticalAlignment="Center"
                                  Grid.RowSpan="2"
                                  Margin="{TemplateBinding Padding}"
                                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
              </Border>
              <Border Background="#FFAAAAAA"
                      BorderBrush="#00FFFFFF"
                      Padding="10 5"
                      BorderThickness="0"
                      Opacity="0"
                      Name="Matte" />
            </Grid>
            <ControlTemplate.Triggers>
              <Trigger Property="IsMouseOver"
                       Value="True">
                <Trigger.ExitActions>
                  <BeginStoryboard Storyboard="{StaticResource HideShine}" />
                </Trigger.ExitActions>
                <Trigger.EnterActions>
                  <BeginStoryboard Storyboard="{StaticResource ShowShine}" />
                </Trigger.EnterActions>
              </Trigger>
              <Trigger Property="IsPressed"
                       Value="True">
                <Setter Property="Opacity"
                        TargetName="Pressed"
                        Value="1" />
              </Trigger>
              <Trigger Property="IsEnabled"
                       Value="False">
                <Trigger.ExitActions>
                  <BeginStoryboard Storyboard="{StaticResource HideMatte}" />
                </Trigger.ExitActions>
                <Trigger.EnterActions>
                  <BeginStoryboard Storyboard="{StaticResource ShowMatte}" />
                </Trigger.EnterActions>
              </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style.Setters>
  </Style>

1 个答案:

答案 0 :(得分:1)

首先是第一件事:

为什么要像<Button.Content> </Button.Content>一样使用它?

完全由

完成
<Button>
   <TextBlock Style="{StaticResource MyStyle}"/>
</Button>

其次,样式本身包含很少的ContentPresenters,因为它应该是每个按钮一个,或者只有其中一个才能Content={TemplateBinding Content}才能显示内容。