WPF--Blend制作Button控件模板--问题补充

 

补充记录Button控件模板

控件模板制作过程中出现下图问题:动画对象不能用于动画属性"Fill”

WPF--Blend制作Button控件模板--问题补充_第1张图片

并且这类问题Blend4中包括VS2010中仍然可以运行,但是只有VS2010中会报错;如下图

模板代码为如下:

 1     <Style x:Key="BtnS2" TargetType="{x:Type Button}">
 2         <Setter Property="Template">
 3             <Setter.Value>
 4                 <ControlTemplate TargetType="{x:Type Button}">
 5                     <Grid>
 6                         <VisualStateManager.VisualStateGroups>
 7                             <VisualStateGroup x:Name="CommonStates">
 8                                 <VisualState x:Name="Normal">
 9                                     <Storyboard>
10                                         <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle">
11                                             <EasingColorKeyFrame KeyTime="0" Value="#00000000"/>
12                                         </ColorAnimationUsingKeyFrames>
13                                     </Storyboard>
14                                 </VisualState>
15                                 <VisualState x:Name="MouseOver">
16                                     <Storyboard>
17                                         <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle">
18                                             <EasingColorKeyFrame KeyTime="0" Value="#FF9DC23E"/>
19                                         </ColorAnimationUsingKeyFrames>
20                                         <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle">
21                                             <EasingColorKeyFrame KeyTime="0" Value="White"/>
22                                         </ColorAnimationUsingKeyFrames>
23                                     </Storyboard>
24                                 </VisualState>
25                                 <VisualState x:Name="Pressed">
26                                     <Storyboard>
27                                         <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle">
28                                             <EasingColorKeyFrame KeyTime="0" Value="#FF659E11"/>
29                                         </ColorAnimationUsingKeyFrames>
30                                         <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle">
31                                             <EasingColorKeyFrame KeyTime="0" Value="White"/>
32                                         </ColorAnimationUsingKeyFrames>
33                                     </Storyboard>
34                                 </VisualState>
35                                 <VisualState x:Name="Disabled"/>
36                             </VisualStateGroup>
37                         </VisualStateManager.VisualStateGroups>
38                         <Rectangle x:Name="rectangle" RadiusY="10" RadiusX="10" Stroke="#00000000" Fill="Black"/>
39                         <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
40                     </Grid>
41                     <ControlTemplate.Triggers>
42                         <Trigger Property="IsFocused" Value="True"/>
43                         <Trigger Property="IsDefaulted" Value="True"/>
44                         <Trigger Property="IsMouseOver" Value="True"/>
45                         <Trigger Property="IsPressed" Value="True"/>
46                         <Trigger Property="IsEnabled" Value="False"/>
47                     </ControlTemplate.Triggers>
48                 </ControlTemplate>
49             </Setter.Value>
50         </Setter>
51     </Style>

引用上述Button模板就会发生VS2010报错,但是运行时没有问题的。

经过反复的尝试,最终发现原来是如下图这段代码导致:

原本这是用于显示模板Normal下的的动画,导致VS2010报错。原本的报错内容在网上也没有找到答案。

最终代码改成了如下:

 1     <Style x:Key="BtnS2" TargetType="{x:Type Button}">
 2         <Setter Property="Template">
 3             <Setter.Value>
 4                 <ControlTemplate TargetType="{x:Type Button}">
 5                     <Grid>
 6                         <VisualStateManager.VisualStateGroups>
 7                             <VisualStateGroup x:Name="CommonStates">
 8                                 <VisualState x:Name="Normal"/>
 9                                 <VisualState x:Name="MouseOver">
10                                     <Storyboard>
11                                         <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle">
12                                             <EasingColorKeyFrame KeyTime="0" Value="#FF9DC23E"/>
13                                         </ColorAnimationUsingKeyFrames>
14                                         <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle">
15                                             <EasingColorKeyFrame KeyTime="0" Value="White"/>
16                                         </ColorAnimationUsingKeyFrames>
17                                     </Storyboard>
18                                 </VisualState>
19                                 <VisualState x:Name="Pressed">
20                                     <Storyboard>
21                                         <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle">
22                                             <EasingColorKeyFrame KeyTime="0" Value="#FF659E11"/>
23                                         </ColorAnimationUsingKeyFrames>
24                                         <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle">
25                                             <EasingColorKeyFrame KeyTime="0" Value="White"/>
26                                         </ColorAnimationUsingKeyFrames>
27                                     </Storyboard>
28                                 </VisualState>
29                                 <VisualState x:Name="Disabled"/>
30                             </VisualStateGroup>
31                         </VisualStateManager.VisualStateGroups>
32                         <Rectangle x:Name="rectangle" RadiusY="10" RadiusX="10" Fill="#00000000" Stroke="#00000000"/>
33                         <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
34                     </Grid>
35                     <ControlTemplate.Triggers>
36                         <Trigger Property="IsFocused" Value="True"/>
37                         <Trigger Property="IsDefaulted" Value="True"/>
38                         <Trigger Property="IsMouseOver" Value="True"/>
39                         <Trigger Property="IsPressed" Value="True"/>
40                         <Trigger Property="IsEnabled" Value="False"/>
41                     </ControlTemplate.Triggers>
42                 </ControlTemplate>
43             </Setter.Value>
44         </Setter>
45     </Style>

去掉模板Normal下的动画内容,VS2010就不会报错。

你可能感兴趣的:(button)