WPF throw new InvalidOperationException(SR.Get(“Storyboard_ImmutableTargetNotSupported“, path.Path))

报错如图:

原因解析:

今天我在写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>

效果如下:成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值