2010-03-04 68 views
2

我有一個ListView(如下所示)。當數值顯示變化時(理想情況下一種顏色增加,一種顏色減少),我希望單元格閃爍一種顏色。當ListView單元格中的值發生變化時閃爍顏色

我知道如何爲顏色編寫動畫(下圖),我很確定我需要使用單元格模板,以便可以將觸發器掛鉤到樣式以啓動動畫。我只是不確定觸發器的最佳位置。

我希望我可以掛鉤到PropertyChanged事件,但我不知道如何。

<ListView ItemsSource="{Binding MyListItems}"> 
     <ListView.View> 
      <GridView>     
       <GridViewColumn Header="Value1" Width="50" CellTemplate="{StaticResource Value1CellTemplate}" /> 
       <GridViewColumn Header="Value2" Width="50" DisplayMemberBinding="{Binding Value2}" /> 
      </GridView> 
     </ListView.View> 
    </ListView> 

電池模板和彩色動畫:

<DataTemplate x:Key="Value1CellTemplate"> 
     <TextBlock Text="{Binding LowerBound}" HorizontalAlignment="Right" /> 
    </DataTemplate> 

    <Storyboard x:Key="IncreaseValueColourAnimation" Duration="0:0:2"> 
     <ColorAnimationUsingKeyFrames> 
      <ColorAnimationUsingKeyFrames.KeyFrames> 
       <LinearColorKeyFrame Value="Red" KeyTime="0:0:0.1" /> 
       <LinearColorKeyFrame Value="Transparent" KeyTime="0:0:2" /> 
      </ColorAnimationUsingKeyFrames.KeyFrames> 
     </ColorAnimationUsingKeyFrames> 
    </Storyboard> 

回答

2

我相信你正在尋找TargetUpdated事件關閉FrameworkElement的。

在目標值的任何屬性此 元件上結合改變 時發生。

然後,您應該能夠使用EventTrigger來運行您的動畫。

+0

乾杯!就是這樣。 – Ray 2010-03-04 16:35:34

+0

我相信本·馮·漢多夫會很感激你將此標記爲答案。 :) – opedog 2010-03-04 18:10:35

1

這是最好的,我可以在有限的時間內拿出我有。如有必要,我可以在明天幫助你。注意,我通過使用CellTemplate的TextBox作弊,這使我能夠使用TextChanged事件來觸發ColorAnimation。我無法讓您的KeyframeAnimation正常工作,所以我使用了ColorAnimation。祝你好運!

<Window x:Class="CellFlashSpike.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300"> 
    <Window.Resources> 
     <Storyboard x:Key="IncreaseValueColourAnimation" 
        Duration="0:0:2" 
        AutoReverse="True"> 
      <ColorAnimation Storyboard.TargetProperty="(TextBox.Foreground).(SolidColorBrush.Color)" 
          To="Red"/> 
     </Storyboard> 
     <DataTemplate x:Key="FlashTemplate"> 
      <TextBox Width="50" Text="{Binding Path=.}"> 
       <TextBox.Style> 
        <Style> 
         <Style.Triggers> 
          <EventTrigger RoutedEvent="TextBox.TextChanged"> 
           <EventTrigger.Actions> 
            <BeginStoryboard> 
             <StaticResource ResourceKey="IncreaseValueColourAnimation"/> 
            </BeginStoryboard> 
           </EventTrigger.Actions> 
          </EventTrigger> 
         </Style.Triggers> 
        </Style> 
       </TextBox.Style> 
      </TextBox> 
     </DataTemplate> 
    </Window.Resources> 
    <Grid> 
     <ListView ItemsSource="{Binding Numbers}"> 
      <ListView.View> 
       <GridView> 
        <GridViewColumn CellTemplate="{StaticResource FlashTemplate}"/> 
       </GridView> 
      </ListView.View> 
     </ListView> 
    </Grid> 
</Window> 

namespace CellFlashSpike 
{ 
    public partial class Window1 : Window 
    { 
     public List<string> Numbers { get; set; } 

     public Window1() 
     { 
      Numbers = new List<string> { "3", "4" }; 
      InitializeComponent(); 
      DataContext = this; 
     } 
    } 
} 
+0

對不起,這是一個寫得不好的動畫,現在已經修復了。 – Ray 2010-03-04 16:18:05

+0

你說得對,TextBox是作弊的。用上面提到的Binding.TargetUpdated屬性獲得它。 – Ray 2010-03-04 16:36:15

相關問題