1

私はプロパティを持っています:

    double _X;
    public double X
    {
        get {
            this.Title = _X.ToString();
            return _X; 
        }
        set { 
            _X = value;
            this.Title = _X.ToString(); // !!!! this line does not execute when property changes from storyboard
            RaisePropertyChanged("X"); }
    }

そしてコントロール:

   <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Height="99.2" Margin="37,14,0,0" VerticalAlignment="Top" Width="98.4" RenderTransformOrigin="0.5,0.5">
        <Button.RenderTransform>
            <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform/>
                <TranslateTransform X="{Binding X}" Y="{Binding Y}"/>
            </TransformGroup>
        </Button.RenderTransform>
    </Button>

絵コンテ:

<Storyboard x:Key="Storyboard1" AutoReverse="True" FillBehavior="HoldEnd">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="button">
            <EasingDoubleKeyFrame KeyTime="0:0:1.2" Value="100">
                <EasingDoubleKeyFrame.EasingFunction>
                    <CircleEase EasingMode="EaseOut"/>
                </EasingDoubleKeyFrame.EasingFunction>
            </EasingDoubleKeyFrame>
        </DoubleAnimationUsingKeyFrames>

</Storyboard>

プロパティをアニメーション化すると、INotifyPropertyChanged インターフェイスを実装している場合に set メソッドが変更されないのはなぜですか? this.Title = _X.ToString();つまり、ストーリーボードのアニメーション化中にラインが実行されないのはなぜですか。ストーリーボードがアニメーション化されるたびに X プロパティを更新するにはどうすればよいですか?


編集

この機能が必要な理由について言及するのを忘れていました。xaml にないプロパティをアニメーション化する必要があることがよくあります。たとえば、マスター ボリュームやマウスの位置をアニメートしたいとします。スレッドを作成し、計算を行って独自のアニメーションを作成できることを知っています。しかし、wpf に既にいくつかのイージング関数とアニメーションがあるのに、なぜ車輪を再発明するのでしょうか。エクスプレッション ブレンドを使用してストーリーボードを作成し、そのストーリーボードを使用してコード ビハインドのプロパティをアニメーション化できればいいと思います。


編集2

@clemensによると、私はこれを解決しました(おそらく私は何か間違ったことをしています):

私はクラスを持っています:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.ComponentModel;
using System.Windows;

namespace SomeNamespace
{
    public partial class MyUserControl : UserControl, INotifyPropertyChanged
    {


        public static readonly DependencyProperty SomeDoubleProperty =
    DependencyProperty.Register("SomeDouble", typeof(Double),
        typeof(MyUserControl), new PropertyMetadata(0.0));


        public double SomeDouble
        {
            get
            {
                return (double)GetValue(SomeDoubleProperty);

            }
            set
            {
                SetValue(SomeDoubleProperty, value);
                MessageBox.Show("this msg should appear meanwhile animating!");
                // code does not execute
                RaisePropertyChanged("SomeDouble");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }




    }
}

私はビューを持っています

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:test="clr-namespace:SomeNamespace"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Window.Resources>
        <Storyboard x:Key="Storyboard1"  >
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="SomeDouble" Storyboard.TargetName="m">
                <EasingDoubleKeyFrame KeyTime="0:0:5.4" Value="100">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <CircleEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>            
        </Storyboard>
    </Window.Resources>
    <Grid>
        <test:MyUserControl x:Name="m" SomeDouble="5"></test:MyUserControl>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="311,50,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />

    </Grid>
</Window>

ボタンのコードビハインドがストーリーボードを開始します...

プロパティは、アニメーション化中にコードビハインドで変更されません...

4

1 に答える 1

4

私があなたを正しく理解していればX、TranslateTransform のプロパティをアニメーション化するつもりです。これは、プロパティにもバインドされてXいます (定義されているクラスについては言及していません)。

双方向バインディングを宣言したとしても、あなたのアプローチは機能しません。などの依存関係プロパティTranslateTransform.Xは、スタイル セッター、トリガー、アニメーション、値の継承、プロパティ メタデータからの既定値、ローカル値など (データ バインディングがローカル値を設定する) など、さまざまなプロバイダーから値を取得します。これらのプロバイダーは、特定の順序、つまり特定の優先順位で適用されます。

あなたのシナリオでは、これはRenderTransform.Xデータバインディングからローカル値を取得することを意味します。アニメーションを開始すると、有効なプロパティ値は、アニメーションが実行中または値を保持している限り、そのアニメーションから取得されます。一方、プロパティのローカル値は変更されません。つまり、バインディングに影響はありません (双方向であっても)。

プロパティをアニメーション化するX場合は、それをカスタム依存関係プロパティにして、直接アニメーション化できます。

于 2012-05-25T08:09:57.787 に答える