【发布时间】:2014-10-14 19:26:22
【问题描述】:
我在我的 wpf 应用程序中为按钮设置了一个控件模板,但在我的情节提要中为 ContentPresenter(即文本)前景色设置动画时遇到了问题。
下面是设置按钮的xaml
<ControlTemplate x:Key="SaveButton" TargetType="{x:Type ButtonBase}">
<Border x:Name="buttonBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Control Grid.Column="0" Template="{DynamicResource save_icon}"
Margin="10,0,0,0"
/>
<TextBlock x:Name="buttonContent"
Text="Save"
Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Grid.Column="1"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsDefaulted" Value="True">
<Setter Property="BorderBrush" TargetName="buttonBorder" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
To="#000" Duration="0:0:0.2"
Storyboard.TargetName="buttonBorder"
Storyboard.TargetProperty="Background.Color"/>
<ColorAnimation
To="#fff" Duration="0:0:0.2"
Storyboard.TargetName="buttonContent"
Storyboard.TargetProperty="TextElement.Foreground"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
To="#fff" Duration="0:0:0.2"
Storyboard.TargetName="buttonBorder"
Storyboard.TargetProperty="Background.Color"/>
<ColorAnimation
To="#000" Duration="0:0:0.2"
Storyboard.TargetName="buttonContent"
Storyboard.TargetProperty="TextElement.Foreground"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
但是在鼠标悬停时出现以下错误 - 无法解析属性路径“TextElement.Foreground”中的所有属性引用
为什么会这样,我该如何解决?
【问题讨论】:
-
显然,您不能为
ContentPresenter上的TextElement.ForegroundAttached 属性设置动画。如果您仅将其用作文本,则只需将其替换为TextBlock即可。 -
所以我用
-
刚刚更新了我的代码以显示从 ContentPresenter 到 Textblock 的变化
标签: wpf xaml animation storyboard contentpresenter