我在我的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'中的所有属性引用
为什么会这样,我该如何解决这个问题?
答案 0 :(得分:1)
<ColorAnimation Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)" To="Red"/>
<ColorAnimation Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" To="Red"/>
怀疑者的完整示例
<Button Content="Context">
<Button.Template>
<ControlTemplate TargetType="Button">
<Border Name="bord" Background="CadetBlue">
<TextBlock Name="tekst" Text="text" Foreground="Black"/>
</Border>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="bord" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="Red"/>
<ColorAnimation Storyboard.TargetName="tekst" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" To="White"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>