0

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

在设计器中看起来像这样:

隐形文字

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>
4

1 回答 1

1

第一件事是第一件事:

你为什么这样使用它<Button.Content> </Button.Content>

它只是由

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

其次,样式本身包含很少的 ContentPresenter,因为它应该是每个按钮一个,或者您可以Content={TemplateBinding Content}只使用其中一个以显示内容。

于 2013-02-08T06:30:51.093 回答