我为我的按钮定义了一个自定义样式。我想将 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>