【问题标题】:Bind storyboard color animation from local string variable从本地字符串变量绑定故事板颜色动画
【发布时间】:2012-12-06 20:10:28
【问题描述】:

在 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;
    }
}

【问题讨论】:

  • 属性必须是字符串吗?或者你可以使用Color?
  • 您确定您使用正确的颜色名称吗?

标签: wpf binding storyboard coloranimation


【解决方案1】:

刚刚使用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>

希望这会有所帮助:)

【讨论】:

  • 感谢您的帮助。你能解释一下为什么你将“UI”作为元素名称传递吗?
  • 我现在已经全部设置好了。我必须将数据上下文设置为 self。
猜你喜欢
  • 2011-03-11
  • 1970-01-01
  • 2012-02-21
  • 2022-01-01
  • 2021-06-30
  • 2021-05-05
  • 2014-01-20
  • 2020-05-19
相关资源
最近更新 更多