2010-06-14 21 views
2

我有一個UserControl,它是其他用戶控件的基類,以「模態視圖」顯示。
我想讓所有用戶控件在顯示時消失,在關閉時消失。我也希望用戶能夠移動控件。我的構造是這樣的:Blend Interaction Behavior給出了「指向不可變實例」的錯誤

var tg = new TransformGroup(); 
tg.Children.Add(new ScaleTransform()); 
RenderTransform = tg; 
var behaviors = Interaction.GetBehaviors(this); 
behaviors.Add(new TranslateZoomRotateBehavior()); 

Loaded += ModalDialogBase_Loaded; 

而且ModalDialogBase_Loaded方法是這樣的:

private void ModalDialogBase_Loaded(object sender, RoutedEventArgs e) 
{ 
    var fadeInStoryboard = (Storyboard)TryFindResource("modalDialogFadeIn"); 
    fadeInStoryboard.Begin(this); 
} 

當我按控制一個關閉按鈕調用此方法:

protected virtual void Close() 
{ 
    var fadeOutStoryboard = (Storyboard)TryFindResource("modalDialogFadeOut"); 
    fadeOutStoryboard = fadeOutStoryboard.Clone(); 
    fadeOutStoryboard.Completed += delegate 
    { 
     RaiseEvent(new RoutedEventArgs(ClosedEvent)); 
    }; 
    fadeOutStoryboard.Begin(this); 
} 

用於淡出的故事板如下所示:

<Storyboard x:Key="modalDialogFadeOut"> 
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="{x:Null}"> 
     <EasingDoubleKeyFrame KeyTime="0" Value="1"> 
      <EasingDoubleKeyFrame.EasingFunction> 
       <BackEase EasingMode="EaseIn" Amplitude="0.3" /> 
      </EasingDoubleKeyFrame.EasingFunction> 
     </EasingDoubleKeyFrame> 
     <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0"> 
      <EasingDoubleKeyFrame.EasingFunction> 
       <BackEase EasingMode="EaseIn" Amplitude="0.3" /> 
      </EasingDoubleKeyFrame.EasingFunction> 
     </EasingDoubleKeyFrame> 
    </DoubleAnimationUsingKeyFrames> 
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="{x:Null}"> 
     <EasingDoubleKeyFrame KeyTime="0" Value="1"> 
      <EasingDoubleKeyFrame.EasingFunction> 
       <BackEase EasingMode="EaseIn" Amplitude="0.3" /> 
      </EasingDoubleKeyFrame.EasingFunction> 
     </EasingDoubleKeyFrame> 
     <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0"> 
      <EasingDoubleKeyFrame.EasingFunction> 
       <BackEase EasingMode="EaseIn" Amplitude="0.3" /> 
      </EasingDoubleKeyFrame.EasingFunction> 
     </EasingDoubleKeyFrame> 
    </DoubleAnimationUsingKeyFrames> 
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="{x:Null}"> 
     <EasingDoubleKeyFrame KeyTime="0" Value="1" /> 
     <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0" /> 
     <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0" /> 
    </DoubleAnimationUsingKeyFrames> 
</Storyboard> 

如果顯示用戶控件,並且用戶不在屏幕上移動它,則一切正常。但是,如果用戶確實將控件移動到了附近,則在啓動modalDialogFadeOut故事板時出現以下錯誤:

'路徑中的兒童屬性值'(0)。(1)[0]。(2) '指向'System.Windows.Media.TransformCollection'的不可變實例。

我該如何解決這個問題?

回答

4

問題是,TranslateZoomRotateBehavior正在用MatrixTransform替換您的ScaleTransform,導致Storyboard中的前兩個動畫以不再存在的屬性爲目標。

由於您不能對Matrix的值進行動畫效果以獲得淡出效果,因此我會使用額外的容器控件。最外面的行爲,然後是內在的視覺淡出。

+0

太棒了!正是我在找的 - 謝謝! – kennethkryger 2010-06-17 09:34:02