2012-12-06 25 views
0

對於在WPF中進行綁定,我仍然是一個業餘愛好者,但我希望能夠在將某個字符串綁定到故事板動畫方面獲得一些幫助。我有一個自定義的UserControl,只有一個TextBox和一些按鈕。我想要做的是每當TextBox從服務器獲取信息時,前景將從淺色變爲深色。在創建此控件時,用戶指定他們想要看到動畫的顏色。作爲一個例子,讓我們一起去淺綠色到深綠色。我在UserControl中有兩個變量存儲爲字符串,現在我想將它們綁定到故事板動畫。任何幫助將不勝感激。從本地字符串變量中綁定故事板彩色動畫

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; 
    } 
} 
+0

執行性質必須是字符串?或者你可以使用'顏色?' –

+0

你確定你使用的顏色是專有名稱? – Paparazzi

回答

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> 

希望這有助於:)

+0

謝謝你的幫助。你能向我解釋爲什麼你傳遞'UI'作爲元素名嗎? – Seb

+0

現在我已經完成了所有設置。我必須將數據上下文設置爲self。 – Seb