2012-02-25 78 views
1

我喜歡使用ItemsControl來託管ContentsControls。每個新的ContentsControl都會在項目被添加時以及每個ContentControl覆蓋前一個內容時爲其內容製作動畫。 ItemsControl和ContentControl內容使用命名約定與Caliburn Micro綁定。所以,現在如何動畫ItemsControl中的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); 
    } 
} 

最後我的問題:

    <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中這樣定義。在真正的ContentControl中,我喜歡使內容動畫,但 在創建Animation的地方調用OnContentChange時,ContentControl的大小爲0。當ContentControl中被ItemsControl中的託管呼叫的命令是:

  1. OnContentChanged(動畫failes)
  2. OnApplyTemplate
  3. 的MeasureOverride

當ContentControl中運行本身的順序是:

  1. OnApplyTemplate
  2. 的MeasureOverride
  3. OnContentChanged(動畫作品)

這裏的問題是,ItemsControl中的新項目的完整的視覺樹是0(DesiredSize,ActualSize = 0),因此我的動畫代碼失敗。 我希望有一定道理給別人, 任何幫助將是巨大的,THX,J

-------------------------- ----修正-------------------

好吧我將OnLoaded事件處理程序添加到DummyControl的ctor。 1. OnContentChanged(所有大小爲0) 2. OnApplyTemplate(所有大小都爲0) 3. MeasureOverride(通過ContentControl對所有子控件hostet調用幾次可能) 4.加載事件(Desired大小被設置爲所有其他大小仍然是0)

可以sitorody解釋什麼推薦的做法是如何動畫ContentControl hostet由ItemsControl?

+0

您是否已驗證在嘗試處理動畫之前加載了虛擬控件?如果沒有加載,你應該推遲動畫。 – BladeWise 2012-02-25 09:30:18

回答

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> 
相關問題