1

ここにいくつかのコードがあります:

<components:AnimatedContentControl x:Name="MainContent" Content="{Binding}">
    <components:AnimatedContentControl.Triggers>
        <EventTrigger RoutedEvent="components:AnimatedContentControl.ContentChanged">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation
                         Storyboard.TargetName="MainContent"
                         Storyboard.TargetProperty="Height"
                         Duration="0:0:0.25"
                         From="0"
                         To="{Binding ElementName=MainContent, Path=ActualHeight}" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </components:AnimatedContentControl.Triggers>
</components:AnimatedContentControl>

AnimatedContentControlは、内部にルーティング イベントがある ContentControl に基づいて作成したクラスですContentChanged(何らかの理由で、 s には既定ContentControlでイベントがないため)。ContentChanged

このコードの目標は、アプリケーションが指すものをすべてDataContextthis にロードしContentControl、データ コンテキストが変更されたときに、ゼロから新しいコンテンツのサイズまでのスライドで構成される単純なアニメーションを表示することです (理想的には、古いコンテンツの高さから新しいコンテンツの高さにスライドしますが、最初のステップが最初です)。このような単純なアイデアに対して、これを開発するのは非常に困難でした。

私の問題が何であるかはすでにわかっています。このイベントが発生すると、 のContentプロパティはAnimatedContentControl更新されていますが、何らかの理由で の は更新されActualSizeAnimatedContentControlいません (実際、考えてみると、おそらくActualSizeプロパティFromDoubleAnimation-後で試します) 。

だから私の質問はこれです:のコンテンツのDoubleAnimation実際のサイズである にバインドできるものはありますか? AnimatedContentControlもしそうなら、それは何ですか?そうでない場合、どのような回避策がありますか?

4

1 に答える 1

0

ボリスは正しかった-私のコードは次のようになります。

<components:AnimateableContentControl x:Name="MainContent" Content="{Binding}">
    <components:AnimateableContentControl.Triggers>
        <EventTrigger RoutedEvent="components:AnimateableContentControl.ContentChanged">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation
                        x:Name="TransitionAnimation"
                        Storyboard.TargetName="MainContent"
                        Storyboard.TargetProperty="Height"
                        Duration="0:0:0.25"
                        From="0"
                        To="0" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </components:AnimateableContentControl.Triggers>
</components:AnimateableContentControl>

...そして、DataContexts を変更した場所 (アニメーションの原因) には、次のものがあります。

DataTemplate template = Resources[new DataTemplateKey(m_currentViewModel.GetType())] as DataTemplate;

if (template == null)
{
    DataContext = m_currentViewModel;
    return;
}

FrameworkElement element = template.LoadContent() as FrameworkElement;

if (element == null)
{
    DataContext = m_currentViewModel;
    return;
}

element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

TransitionAnimation.To = element.DesiredSize.Height;

DataContext = m_currentViewModel;

魔法のように動作します!ありがとう、ボリス!

于 2013-08-28T00:14:15.813 に答える