我喜欢使用 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 中时的调用顺序为:
当 ContentControl 自行运行时,顺序为:
这里的问题是 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/