0

私は WPF でのバインドに関してはまだ少し素人ですが、文字列をストーリーボード アニメーションにバインドすることに関して助けが得られることを望んでいました。TextBox といくつかのボタンが配置されたカスタム UserControl があります。私がやりたいことは、前景が明るい色から暗い色にアニメーション化されるというサーバーからの情報を TextBox が取得するたびです。このコントロールの作成時に、ユーザーはアニメーションで表示する色を指定します。例として、薄緑から濃い緑に行きましょう。UserControl 内に文字列として格納されている 2 つの変数があり、それらをストーリーボード アニメーションにバインドしたいと考えています。どんな助けでも大歓迎です。

XAML:

<EventTrigger RoutedEvent="TextBox.TextChanged">
    <BeginStoryboard>
        <Storyboard>
            <ColorAnimation AutoReverse="False" Duration="0:0:2" From="{Binding StartTextColor}" To="{Binding EndTextColor}"
                Storyboard.TargetName="txtTextField" AccelerationRatio="1" 
                Storyboard.TargetProperty="(TextBox.Foreground).(SolidColorBrush.Color)"
                 FillBehavior="HoldEnd">
            </ColorAnimation>
        </Storyboard>
    </BeginStoryboard>
</EventTrigger>

コード:

public string StartTextColor
{
    get
    {
        return startTextColor;
    }
    set
    {
        startTextColor= value;
    }
}

public string EndTextColor
{
    get
    {
        return _endTextColor;
    }
    set
    {
        _endTextColor= value;
    }
}
4

1 に答える 1

3

stringアニメーションにカラー バインディングを使用して簡単なテストを行ったところ、正常に動作しました。バインドする場合はINotifyPropertyChanged、プロパティが変更されたことを XAML に通知するため、オブジェクトが実装されていることを確認する必要があります。

私のテスト:

  public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private string startTextColor = "Green";
        private string _endTextColor = "Red";

        public MainWindow()
        {
            InitializeComponent();
        }

        public string StartTextColor
        {
            get { return startTextColor; }
            set
            {
                startTextColor = value;
                NotifyPropertyChanged("StartTextColor");
            }
        }

        public string EndTextColor
        {
            get { return _endTextColor; }
            set
            {
                _endTextColor = value;
                NotifyPropertyChanged("EndTextColor");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// Notifies the property changed.
        /// </summary>
        /// <param name="info">The info.</param>
        public void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

    }

Xaml:

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication4"
        Title="MainWindow" Height="350" Width="2640" Name="UI" >
        <Grid>
            <TextBox Name="txtbx">
              <TextBox.Triggers>
                <EventTrigger RoutedEvent="TextBox.TextChanged">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation AutoReverse="False" Duration="0:0:2" AccelerationRatio="1" Storyboard.TargetName="txtbx" FillBehavior="HoldEnd" 
                                            From="{Binding ElementName=UI, Path=StartTextColor}" 
                                            To="{Binding ElementName=UI, Path=EndTextColor}"
                                            Storyboard.TargetProperty="(TextBox.Foreground).(SolidColorBrush.Color)">
                            </ColorAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
              </TextBox.Triggers>
            </TextBox>
        </Grid>
</Window>

お役に立てれば :)

于 2012-12-06T20:31:08.817 に答える