我已经在 Expression Blend 中为一个按钮拼凑了一个模板,它工作正常 - 直到我切换到它并按下空格键。我收到以下错误:
InvalidOperationException '[Unknown]' 属性未指向路径 '(0).(1).[0].(2)' 中的 DependencyObject。
代码如下:
<ControlTemplate x:Key="EmailButton" TargetType="{x:Type Button}">
<ControlTemplate.Resources>
<Storyboard x:Key="MouseOverStoryboard">
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
<SplineColorKeyFrame KeyTime="00:00:00.4000000" Value="#FF55679E"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="UnMouseOverStoryboard">
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
<SplineColorKeyFrame KeyTime="00:00:00.4000000" Value="#FF92A2D3"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<Border x:Name="border" BorderBrush="#FF000000" BorderThickness="2,2,2,2" CornerRadius="4,4,4,4">
<Border.Background>
<LinearGradientBrush EndPoint="0.47,-0.01" StartPoint="0.47,0.808">
<GradientStop Color="#FF92A2D3" Offset="0"/>
<GradientStop Color="#FFFFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="{TemplateBinding Padding}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" TargetName="border" Value="#FF92A2D3"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard x:Name="MouseOverStoryboard_BeginStoryboard" Storyboard="{StaticResource MouseOverStoryboard}"/>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard x:Name="UnMouseOverStoryboard_BeginStoryboard" Storyboard="{StaticResource UnMouseOverStoryboard}"/>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
似乎 MouseOverStoryboard 或 UnMouseOverStoryboard 被触发(错误中提到的路径似乎与 TargetProperty 匹配)。我不确定为什么在按下空格键时会触发...有什么建议吗?
编辑:感谢 Ryan Versaw,我尝试了以下方法,现在可以防止空格键单击引发异常:
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" TargetName="border">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF92A2D3" Offset="0"/>
<GradientStop Color="#FF92A2D3" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
现在画笔类型保持一致(LinearGradientBrush),并且始终有第 0 个 GradientStop 可用。
我仍然对另一件事感到好奇 -考虑到这种感觉就像一个 hack(例如:添加/动画第三个渐变停止,然后在 IsPressed 时需要添加第三个 GradientStop,那么最好的方法是什么? ==真)。
感谢您的建议!希望这对其他人也有帮助。