我喜欢使用ItemsControl来托管ContentsControls。每个新的ContentsControl都会在添加该项目,每个ContentControl并覆盖前一个内容时对其内容进行动画处理。使用命名约定,Caliburn Micro绑定(bind)了ItemsControl和ContentControl Content。

                    <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中,我喜欢为内容设置动画,但是
在创建我的Animation的位置调用OnContentChange时,ContentControl的大小为0。当ContentControl托管在ItemsControl中时,调用顺序为:
  • OnContentChanged(动画失败)
  • OnApplyTemplate
  • MeasureOverride

  • 当ContentControl本身运行时,顺序为:
  • OnApplyTemplate
  • MeasureOverride
  • OnContentChanged(动画作品)

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

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

    好的,我将OnLoaded事件处理程序添加到了DummyControl的ctor中。被调用者的顺序是
    1. OnContentChanged(所有大小均为0)
    2. OnApplyTemplate(所有大小均为0)
    3. MeasureOverride(ContentControl可能对所有子控件主机调用了几次)
    4.加载事件(“期望大小”已设置,所有其他大小仍为0)

    Sombody可以解释关于如何为ContentControl设置动画的推荐做法吗
    由ItemsControl进行主机?

    最佳答案

    只需执行XAML中的所有操作,然后让动画完成操作即可,而无需调用MeasureOverride()和其余的挂钩。

    <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/

    10-13 07:05