1

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

                    <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 中,我喜欢为 Content 设置动画,但是在创建我的 Animation 的地方调用 OnContentChange 时,ContentControl 的大小为 0。ContentControl 托管在 ItemsControl 中时的调用顺序为:

  1. OnContentChanged(动画失败)
  2. OnApply 模板
  3. 测量覆盖

当 ContentControl 自行运行时,顺序为:

  1. OnApply 模板
  2. 测量覆盖
  3. OnContentChanged(动画作品)

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

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

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

有人可以解释关于如何通过 ItemsControl 为 ContentControl hostet 设置动画的推荐做法吗?

4

1 回答 1

0

只需在 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>
于 2012-03-29T16:12:38.280 回答