wpf用户控制状态,修改高度



我对WPF中的状态和动画有点困惑。

我想制作一个用户控件。此用户控件将包含(在主网格内)2个其他网格。其中一个将是HEADER,第二个将是CONTENT。如果用户点击标题,内容将展开,否则将被折叠。我想动画扩展(从标题向下滑动内容)。

Basicaly我希望各州都这样做(为了未来的目的)。问题是,如果我添加状态,并且我使用幻灯片效果进行转换,那么这个网格(内容网格)的内容也会进行转换。所以我想使用状态来修改元素的高度。如果只修改元素,则不会显示任何动画,并且只会一次更改其高度。

层次结构如下所示:
---包装网格
------标题网格
---------页眉内容
------内容网格
---------内容网格的内容(如按钮、标签等)

可视化状态如下:

    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="VisualStateGroup">
            <VisualStateGroup.Transitions>
                <VisualTransition GeneratedDuration="0:0:1"/>
            </VisualStateGroup.Transitions>
            <VisualState x:Name="Expanded"/>
            <VisualState x:Name="Collapsed">
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="grid">
                        <DiscreteObjectKeyFrame KeyTime="0">
                            <DiscreteObjectKeyFrame.Value>
                                <x:Double>0</x:Double>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

有什么建议可以通过各州修改高度来查看网格的扩展和折叠?对于动画来说,它只是完美的,但对我来说,为了未来的目的,最好是用状态来做。

也许我找到了答案。目前它是有效的,但我正在努力理解这种变化。

此处的可视化状态代码:

    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="VisualStateGroup">
            <VisualStateGroup.Transitions>
                <VisualTransition GeneratedDuration="0"/>
            </VisualStateGroup.Transitions>
            <VisualState x:Name="Expanded">
                <Storyboard>
                    <DoubleAnimation EnableDependentAnimation="True" Storyboard.TargetName="grid" Storyboard.TargetProperty="(FrameworkElement.Height)" From="0" To="366" Duration="0:0:0.600" />
                </Storyboard>
            </VisualState>
            <VisualState x:Name="Collapsed">
                <Storyboard>
                    <DoubleAnimation EnableDependentAnimation="True" Storyboard.TargetName="grid" Storyboard.TargetProperty="(FrameworkElement.Height)" From="366" To="0" Duration="0:0:0.600" />
                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

重要的一行是:

<DoubleAnimation EnableDependentAnimation="True" Storyboard.TargetName="grid" Storyboard.TargetProperty="(FrameworkElement.Height)" From="366" To="0" Duration="0:0:0.600" />

我发现需要将EnableDependentAnimation设置为true。

现在它是一种魅力,但我不喜欢《从零开始》的布景。但这可能是我在这里能做的最好的事情了。

最新更新