报错如图:
原因解析:
今天我在写Calender自定义控件的时候,遇到的问题
是因为他在变化按钮颜色的时候用的是Visual动画,而我又加个Trigger导致的:
动画和事件会导致报这个错误:throw new InvalidOperationException(SR.Get("Storyboard_ImmutableTargetNotSupported", path.Path));
代码如下:
<ControlTemplate x:Key="PreviousButtonTemplate" TargetType="{x:Type Button}">
<Grid Cursor="Hand">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation Duration="0" Storyboard.TargetName="path" To="#FF73A9D8" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimation Duration="0" Storyboard.TargetName="path" To=".5" Storyboard.TargetProperty="(Shape.Fill).(Brush.Opacity)"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle Fill="Transparent" Opacity="1" Stretch="Fill"/>
<Grid>
<Path x:Name="path"
Data="M288.75,232.25 L288.75,240.625 L283,236.625 z"
Fill="Green"
HorizontalAlignment="Left"
Height="10"
Margin="14,-6,0,0"
Stretch="Fill"
VerticalAlignment="Center"
Width="6"/>
</Grid>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="path" Property="Fill" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
注释掉任何一个都可以 ,我注释掉动画 留下Trigger
如下:
<ControlTemplate x:Key="PreviousButtonTemplate" TargetType="{x:Type Button}">
<Grid Cursor="Hand">
<!--<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation Duration="0" Storyboard.TargetName="path" To="#FF73A9D8" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimation Duration="0" Storyboard.TargetName="path" To=".5" Storyboard.TargetProperty="(Shape.Fill).(Brush.Opacity)"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>-->
<Rectangle Fill="Transparent" Opacity="1" Stretch="Fill"/>
<Grid>
<Path x:Name="path"
Data="M288.75,232.25 L288.75,240.625 L283,236.625 z"
Fill="Green"
HorizontalAlignment="Left"
Height="10"
Margin="14,-6,0,0"
Stretch="Fill"
VerticalAlignment="Center"
Width="6"/>
</Grid>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="path" Property="Fill" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
效果如下:成功