wpf - 如何在 ItemsControl 中为 ContentControl 设置动画

标签 wpf mvvm itemscontrol contentcontrol caliburn

我喜欢使用 ItemsControl 来托管 ContentsControls。添加项目时,每个新的 ContentsControl 都会为其内容设置动画,并且每个 ContentControl 都会覆盖前一个。 ItemsControl 和 ContentControl Content 使用命名约定与 Caliburn Micro 绑定(bind)。

                    <ItemsControl x:Name="OverlayStackedItems" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="Transparent">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <Grid x:Name="ItemsHost" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>

                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <cc:DummyContentControl cal:View.Model="{Binding}" />
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>

ContentControl 的定义如下:
   [ContentProperty("Content")]
public partial class DummyContentControl :ContentControl
{
    public DummyContentControl()
    {
    }

    static DummyContentControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(DummyContentControl), new FrameworkPropertyMetadata(typeof(ContentControl)));
    }


    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
    }

    protected override void OnContentChanged(object oldContent, object newContent)
    {
        LayoutUpdated += (sender, e) => 
        { 
        };
        UpdateLayout();

        base.OnContentChanged(oldContent, newContent);
    }

    void DummyContentControl_LayoutUpdated(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }

    protected override Size MeasureOverride(Size constraint)
    {
        return base.MeasureOverride(constraint);
    }
}

所以现在终于我的问题了。在真正的 ContentControl 中,我喜欢为内容设置动画,但是
当 OnContentChange 在创建我的动画的位置被调用时,ContentControl 的大小为 0。 ContentControl 托管在 ItemsControl 中时的调用顺序为:
  • OnContentChanged(动画失败)
  • OnApply 模板
  • 测量覆盖

  • 当 ContentControl 自行运行时,顺序为:
  • OnApply 模板
  • 测量覆盖
  • OnContentChanged(动画作品)

  • 这里的问题是 ItemsControl 中新项目的完整可视子树为 0 (DesiredSize,ActualSize = 0),因此我的动画代码失败。
    我希望这对某人有意义,
    任何帮助都会很棒,Thx,J

    - - - - - - - - - - - - - - - 修订 - - - - - - - - - -

    好的,我将 OnLoaded 事件处理程序添加到 DummyControl 的 ctor 中。调用顺序是
    1. OnContentChanged(所有尺寸均为0)
    2. OnApplyTemplate(所有尺寸均为0)
    3. MeasureOverride(可能由 ContentControl 为所有子控件主机调用多次)
    4. 加载事件(Desired Size 设置为所有其他大小仍为 0)

    有人能解释一下关于如何为 ContentControl 设置动画的推荐做法吗
    由 ItemsControl 主持?

    最佳答案

    只需在 XAML 中完成所有操作,然后让动画完成,无需调用 MeasureOverride() 和其他钩子(Hook)。

    <ItemsControl>
        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Border>
                                <TextBlock Text="Whatever your template should look like"/>
                            </Border>
                            <ControlTemplate.Triggers>
                                <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                                    <BeginStoryboard>
                                        <Storyboard >
                                            <DoubleAnimation Storyboard.TargetProperty="(Border.RenderTransform).(ScaleTransform.ScaleX)" Duration="0:0:0.5" From="0" To="1" />
                                            <DoubleAnimation Storyboard.TargetProperty="(Border.RenderTransform).(ScaleTransform.ScaleY)" Duration="0:0:0.5" From="0" To="1" />
                                            <DoubleAnimation Storyboard.TargetProperty="(Border.RenderTransform).(ScaleTransform.CenterX)" Duration="0:0:0.5" To="25" />
                                            <DoubleAnimation Storyboard.TargetProperty="(Border.RenderTransform).(ScaleTransform.CenterY)" Duration="0:0:0.5" To="25" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ItemsControl.ItemContainerStyle>
    </ItemsControl>
    

    关于wpf - 如何在 ItemsControl 中为 ContentControl 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9440392/

    相关文章:

    c# - 处理 Caliburn Micro 的静态 IoC

    WPF 4.5 微软功能区 : which control of RibbonApplicationMenu

    wpf - ItemsControl 按钮单击命令

    WPF 数据模板 : How to create a tooltip just-in-time?

    c# - 在 ItemsControl DataTemplate 中设置 Canvas 属性

    wpf - [Multi]DataTrigger "OR"语句?

    c# - 停止 WPF 动画, Storyboard 在 xaml 中开始但在代码隐藏中停止?

    c# - 更改 ModelView 中的样式(MVVM + WPF)

    wpf - Caliburn.Micro + Autofac 引导

    javascript - 嵌套的 vue.js 实例/元素