我在Expression Blend中爲一個按鈕投了一個模板,它工作正常 - 直到我選中它並按空格鍵。我收到以下錯誤:WPF:按鈕ControlTemplate +空格鍵=異常?
InvalidOperationException '[未知]'屬性未指向路徑'(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)。我不知道爲什麼要按下空格鍵時會觸發......任何建議?
編輯: 感謝瑞安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。考慮到這種感覺就像是一種黑客(例如:第三個漸變停止被添加/動畫,然後第三個漸變停止(GradientStop)),我們仍然對另一件事感到好奇 - 什麼是最好的當IsPressed == True時需要添加)。
感謝您的建議!希望這也能幫助別人。
爲了給出應有的功勞,我嘗試用Pwninstein的答案替換背景。其他方法可能會起作用,但這對我有效。 – 2009-05-19 19:02:43