.net ListView单元格中的值更改时闪烁颜色
我有一个列表视图(如下所示)。我希望单元格在显示值改变时闪烁一种颜色(理想情况下,一种颜色表示增加,一种颜色表示减少) 我知道如何为颜色(如下)编写动画,而且我非常确定我需要使用一个单元格模板,这样我就可以将触发器挂接到样式上以开始动画。我只是不确定把扳机挂在什么地方最好 我希望我能参与到PropertyChanged事件中,但我不知道怎么做.net ListView单元格中的值更改时闪烁颜色,.net,wpf,.net,Wpf,我有一个列表视图(如下所示)。我希望单元格在显示值改变时闪烁一种颜色(理想情况下,一种颜色表示增加,一种颜色表示减少) 我知道如何为颜色(如下)编写动画,而且我非常确定我需要使用一个单元格模板,这样我就可以将触发器挂接到样式上以开始动画。我只是不确定把扳机挂在什么地方最好 我希望我能参与到PropertyChanged事件中,但我不知道怎么做 <ListView ItemsSource="{Binding MyListItems}"> <ListView
<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>
我相信您正在寻找
TargetUpdated
事件脱离框架元素
在目标值更改时发生
对于此上的任何属性绑定
元素
然后,您应该能够使用EventTrigger来运行动画。这是我在有限的可用时间内所能想到的最好方法。如有必要,我明天可以进一步帮助你。记住,我欺骗了CellTemplate使用文本框,这使我能够使用TextChanged事件触发ColorAnimation。我无法使您的KeyframeAnimation正常工作,因此我使用了彩色动画。祝你好运
<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;
}
}
}
命名空间单元FlashSpike
{
公共部分类Window1:Window
{
公共列表编号{get;set;}
公共窗口1()
{
数字=新列表{“3”,“4”};
初始化组件();
DataContext=this;
}
}
}
Sorry是一个写得很糟糕的动画,现在有问题了。你说得对,TextBox在作弊。使用上面建议的Binding.TargetUpdated属性获得了它。我相信Ben Von Handorf将非常感谢您将此标记为答案。:)