10

わかりました、私は本当に不意を突かれる何かに出くわしました。

私は、関係のないいくつかの質問で仲間の開発者を助けていました。彼のプロジェクトでは、テキストをいくつかの TextBlock にアニメーション化していました。そこで、デスクに戻ってプロジェクトを再作成しました (彼の質問に答えるために) が、誤ってTextBlockの代わりにTextBoxを使用してしまいました。テキストがまったくアニメーション化されませんでした。(私はとても助かりました!)

最終的に、彼の xaml は TextBlock を使用しており、私のものは TextBox を使用していることがわかりました。興味深いのは、TextBox を使用していたときに Blend がキー フレームを作成していなかったことです。そのため、TextBlock(s) を使用して Blend で動作するようにし、xaml を手動で変更して、TextBlock(s) を TextBox(es) に変換しました。プロジェクトを実行すると、次のエラーが発生しました。

InvalidOperationException: '(0)' Storyboard.TargetProperty path contains nonanimatable property 'Text'.

まあ、Blend はそれを知っているほど賢く、アニ​​メーションでキー フレームを生成しないようです (TextBox の値を直接変更するだけです)。ブレンドの場合は+1。

それで、問題は次のようになりました: TextBox.Text がアニメーション化できないのはなぜですか? 通常の答えは、アニメーション化している特定のプロパティは DependencyProperty ではないというものです。しかし、そうではありません。TextBox.TextDependencyProperty です。

だから、今私は当惑しています!TextBox.Text をアニメーション化できないのはなぜですか?


問題を説明するためにいくつかの xaml を含めましょう。次の xaml は機能しますが、TextBlock を使用します。

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="TextBoxTextQuestion.MainWindow"
    x:Name="Window"
    Title="MainWindow"
    Width="640"
    Height="480"
>
    <Window.Resources>
        <Storyboard x:Key="animateTextStoryboard">
            <StringAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Text)" Storyboard.TargetName="textControl">
                <DiscreteStringKeyFrame KeyTime="0:0:1" Value="Goodbye"/>
            </StringAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Storyboard="{StaticResource animateTextStoryboard}"/>
        </EventTrigger>
    </Window.Triggers>
    <Grid x:Name="LayoutRoot">
        <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBlock x:Name="textControl" Text="Hello" FontFamily="Calibri" FontSize="32"/>
            <TextBlock Text="World!" Margin="0,25,0,0" FontFamily="Calibri" FontSize="32"/>
        </StackPanel>
    </Grid>
</Window>

次の xamlは機能せず、TextBox.Text を使用します。

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="TextBoxTextQuestion.MainWindow"
    x:Name="Window"
    Title="MainWindow"
    Width="640"
    Height="480"
>
    <Window.Resources>
        <Storyboard x:Key="animateTextStoryboard">
            <StringAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBox.Text)" Storyboard.TargetName="textControl">
                <DiscreteStringKeyFrame KeyTime="0:0:1" Value="Goodbye"/>
            </StringAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Storyboard="{StaticResource animateTextStoryboard}"/>
        </EventTrigger>
    </Window.Triggers>
    <Grid x:Name="LayoutRoot">
        <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBox x:Name="textControl" Text="Hello" FontFamily="Calibri" FontSize="32"/>
            <TextBox Text="World!" Margin="0,25,0,0" FontFamily="Calibri" FontSize="32"/>
        </StackPanel>
    </Grid>
</Window>
4

1 に答える 1

28

TextBox を手動でアニメーション化しようとしています....

var timeline = new StringAnimationUsingKeyFrames();
timeline.KeyFrames.Add(new DiscreteStringKeyFrame("Goodbye", KeyTime.FromTimeSpan(new TimeSpan(0,0,1))));
textControl.BeginAnimation(TextBox.TextProperty, timeline);

...より有用なエラー メッセージが表示されます。最後の行は次のエラーで失敗しますArgumentException:

プロパティをクラスに関連付けるために使用される UIPropertyMetadata に IsAnimationProhibited フラグが設定されているため、「System.Windows.Controls.TextBox」クラスで「Text」プロパティをアニメーション化できません。
パラメータ名:dp

UIPropertyMetadata.IsAnimationProhibitedのドキュメントには次のように書かれています。

一般に、Windows Presentation Foundation (WPF) フレームワーク実装 API で使用できる既定の依存関係プロパティはアニメーション化できます。独自のカスタム依存関係プロパティのメタデータでこのプロパティを true に設定して、アニメーションを無効にすることができます。

どうやら、WPF ライブラリの設計者はText、TextBox の依存関係プロパティをアニメーション化することはお勧めできないと判断し、明示的に無効にしました。

これが、このプロパティをアニメーション化できない理由に関する技術的な答えです。なぜ彼らはそれを無効にしたのですか? 何も思いつきません...

PS: の静的コンストラクターをざっと見て、TextBoxReflectorを使用するTextBoxBaseと、それがアニメーション化できない唯一の TextBox 依存関係プロパティであることがControlわかります。Text

于 2010-04-08T22:25:25.937 に答える