0

关于 WPF 中的绑定,我仍然有点业余,但我希望我能在将字符串绑定到情节提要动画方面获得一些帮助。我有一个自定义的 UserControl,仅包含一个 TextBox 和一些按钮。我想要做的是每当 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));
            }
        }

    }

xml:

<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 回答