ControlTemplate中的WPF Grid.Background的ColorAnimation的InvalidOperationException

时间:2016-09-12 07:48:48

标签: wpf background-color controltemplate invalidoperationexception coloranimation

我自定义股票WPF GroupBox控件。当鼠标指针进入控制区域时,我需要为其背景实现彩色动画 - 比如,将背景颜色慢慢地更改为预定义的颜色(让它变成粉红色)。我为此创建了一个自定义控件模板,其基本部分如下所示:

<ControlTemplate TargetType="{x:Type GroupBox}">
    <Grid Name="MainGrid" SnapsToDevicePixels="true">
        <!-- Control layout stuff with ContentPresenter -->
    </Grid>

    <ControlTemplate.Triggers>
        <EventTrigger RoutedEvent="MouseEnter">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation
                        Storyboard.TargetName="MainGrid"
                        Storyboard.TargetProperty="Background.(SolidColorBrush.Color)"
                        To="Pink" Duration="0:0:1" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

然而,我无法使这个动画发挥作用。我总是得到一个未处理的类型异常:

  

&#39; System.InvalidOperationException&#39;发生在   PresentationFramework.dll包含以下附加信息:   &#39;背景&#39; property不指向路径中的DependencyObject   &#39;背景(0)&#39;

我用Google搜索了这个问题。看来,我需要使用TargetProperty的正确语法进行动画处理。但是,我尝试了很多变体,如下面的变体,它们在我的情况下都不起作用:

  • Background.Color
  • (Panel.Background).Color
  • (Panel.Background).(SolidColorBrush.Color)
  • (Grid.Background).(SolidColorBrush.Color)

我的搜索方向不正确吗?

1 个答案:

答案 0 :(得分:1)

您的TargetElement需要其样式中的初始Setter才能让动画生效。

示例

<GroupBox>
        <GroupBox.Style>
            <Style TargetType="GroupBox">
                <Style.Setters>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupBox}">
                                <Grid Name="MainGrid" SnapsToDevicePixels="true">
                                    <Grid.Style>
                                        <Style TargetType="Grid">
                                            <Setter Property="Background" Value="Blue"></Setter>
                                        </Style>
                                    </Grid.Style>
                                    <ContentPresenter/>
                                </Grid>
                                <ControlTemplate.Triggers>
                                    <EventTrigger RoutedEvent="MouseEnter">
                                        <BeginStoryboard>
                                            <Storyboard>
                                                <ColorAnimation Storyboard.TargetName="MainGrid" Storyboard.TargetProperty="Background.Color" To="Pink" Duration="0:0:1" />
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </EventTrigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style.Setters>
            </Style>
        </GroupBox.Style>
        <Button Content="test" Width="200" Height="50"></Button>
    </GroupBox>

执行此操作后,您可以轻松使用Storyboard.TargetProperty="Background.Color"