更改绑定属性的值时如何为文本块的背景设置动画?

2023-12-23

我有一个非常简单的 wpftoolkit:da​​tagrid 来显示股票市场的出价和要价。

我的网格绑定到ObservableCollection<PriceViewModel>. My PriceViewModel实施INotifyPropertyChanged.

网格正确更新,我已经设法使背景颜色具有动画效果,但在应用动画时它是间歇性的。

下面是视图模型类的 XAML 和片段。

这个想法只是当价格更新低于之前的价格时将颜色设为红色,当价格更新的价格高于之前时将颜色设为绿色……没什么太花哨的。

     <WpfToolkit:DataGrid Name="PriceDataGrid" RowHeaderWidth="5" 
AutoGenerateColumns="False" VerticalContentAlignment="Center" Margin="0,33,0,0" HorizontalAlignment="Left" Width="868">
        <WpfToolkit:DataGrid.Columns>
            <WpfToolkit:DataGridTemplateColumn Header="Bid"  MinWidth="40">
                <WpfToolkit:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Bid}" Margin="3,1" x:Name="txtTextBlock">
                            <TextBlock.Background>
                                <SolidColorBrush Color="Transparent"></SolidColorBrush>
                            </TextBlock.Background>
                        </TextBlock>
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding BidUp}" Value="True">
                                <DataTrigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <ColorAnimation 
                                                BeginTime="00:00:00"
                                                Duration="0:0:0.1" 
                                                To="Green" 
                                                AutoReverse="True"
                                                Storyboard.TargetName="txtTextBlock" 
                                                Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)">
                                            </ColorAnimation>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.EnterActions>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding BidDown}" Value="True">
                                <DataTrigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <ColorAnimation 
                                                BeginTime="00:00:00"
                                                Duration="0:0:0.1" 
                                                To="Red" 
                                                AutoReverse="True"
                                                Storyboard.TargetName="txtTextBlock" 
                                                Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)">
                                            </ColorAnimation>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.EnterActions>
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </WpfToolkit:DataGridTemplateColumn.CellTemplate>
            </WpfToolkit:DataGridTemplateColumn>
            <WpfToolkit:DataGridTextColumn Header="Ask" Binding="{Binding Path=Ask}" MinWidth="40" />
        </WpfToolkit:DataGrid.Columns>
    </WpfToolkit:DataGrid>

和视图模型:

public class PriceViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    Price _price;

    private bool _bidUp = false;
    private bool _bidDown = false;


    public bool BidUp
    {
        get
        {
            return _bidUp;
        }

        set
        {
            _bidUp = value;
            OnPropertyChanged("BidUp");
        }
    }
    public bool BidDown
    {
        get
        {
            return _bidDown;
        }

        set
        {
            _bidDown = value;
            OnPropertyChanged("BidDown");
        }
    }

    public double Bid 
    { 
        get { return _price.Bid; }
        set
        {
            BidUp = (value > _price.Bid);
            BidDown = (value < _price.Bid);

            _price.Bid = value; 
            OnPropertyChanged("Bid");
        } 
    }

    public double Ask 
    { 
        get { return _price.Ask; } 
        set 
        {
            AskUp = (value > _price.Ask);
            _price.Ask = value; 
            OnPropertyChanged("Ask"); 
        } 
    }


    public PriceViewModel(Price price)
    {
        _price = price;
    }

    private void OnPropertyChanged(string propertyName)
    {
        if(PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

}

我尝试过这个,如果你停止它似乎效果更好Storyboard在开始新的一项之前。停止一个Storyboard,命名并调用

<StopStoryboard BeginStoryboardName="bidUpStoryboard"/>

像这样尝试一下

<DataTemplate.Triggers>
    <DataTrigger Binding="{Binding BidUp}" Value="True">
        <DataTrigger.EnterActions>
            <StopStoryboard BeginStoryboardName="bidUpStoryboard"/>
            <StopStoryboard BeginStoryboardName="bidDownStoryboard"/>
            <BeginStoryboard Name="bidUpStoryboard">
                <Storyboard BeginTime="00:00:00">
                    <ColorAnimation 
                        BeginTime="00:00:00"
                        Duration="0:0:0.1" 
                        To="Green" 
                        AutoReverse="True"
                        Storyboard.TargetName="txtTextBlock" 
                        Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)">
                    </ColorAnimation>
                </Storyboard>
            </BeginStoryboard>
        </DataTrigger.EnterActions>
    </DataTrigger>
    <DataTrigger Binding="{Binding BidDown}" Value="True">
        <DataTrigger.EnterActions>
            <StopStoryboard BeginStoryboardName="bidUpStoryboard"/>
            <StopStoryboard BeginStoryboardName="bidDownStoryboard"/>
            <BeginStoryboard Name="bidDownStoryboard">
                <Storyboard BeginTime="00:00:00">
                    <ColorAnimation 
                        BeginTime="00:00:00"
                        Duration="0:0:0.1" 
                        To="Red" 
                        AutoReverse="True"
                        Storyboard.TargetName="txtTextBlock" 
                        Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)">
                    </ColorAnimation>
                </Storyboard>
            </BeginStoryboard>
        </DataTrigger.EnterActions>
    </DataTrigger>
</DataTemplate.Triggers>

另外,如果BidUp连续两次设置为 true ,第二次不会触发,因为它会从 true 变为 true,所以如果你希望每次值更改时都出现闪烁效果,则必须将其设置为 false某个点。例如

public double Bid
{
    get { return _price.Bid; }
    set
    {
        BidUp = false;
        BidDown = false;
        BidUp = (value > _price.Bid);
        BidDown = (value < _price.Bid);
        _price.Bid = value;
        OnPropertyChanged("Bid"); }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

更改绑定属性的值时如何为文本块的背景设置动画? 的相关文章

随机推荐

  • Nuxt.js Hackernews API 更新帖子,每分钟无需加载页面

    我有一个 nuxt js 项目 https github com AzizxonZufarov newsnuxt2 https github com AzizxonZufarov newsnuxt2我需要每分钟更新 API 的帖子而不加载页
  • pandaboard 上静态链接的 OpenCV 编译问题

    我使用 BUILD SHARED LIBS 0 选项在 pandaboard 上安装了带有静态链接的 opencv 我使用的是ubuntu 11 10 我还构建了第 3 方库 png jpeg jasper 和 zlib cmake 选项
  • `std::make_pair` 中的 c++11 右值引用

    对于 C 98 template
  • SolrJ 线程安全

    我在 Web 应用程序中使用 CommonsHttpSolrServer 在多个请求中重用 CommonsHttpSolrServer 是否安全 还是应该为每个请求实例化一个新对象 无法在 API 文档中找到答案 根据文档 http wik
  • 从用户输入中读取整数

    我正在寻找的是如何读取用户从命令行 控制台项目 给出的整数 我主要了解 C 并开始走 C 道路 我知道 Console ReadLine 只需要一个字符 字符串 简而言之 我正在寻找它的整数版本 只是为了让您了解我正在做什么 Console
  • Google Calendar API 阻止 Django 启动

    我有一个 Django 1 9 1 项目 它工作得很好 直到我添加了提供的代码Google 日历 API 文档 https developers google com google apps calendar quickstart pyth
  • C++ 中未评估的上下文是什么?

    我经常想到的一个例子是 sizeof表达式 它不计算表达式 而是通过静态类型确定大小 例如 int func sizeof func 这是我的思维极限 所以如果还有其他未评估的上下文 那么它们是什么呢 幸运的是 该标准有一个方便的列表 5
  • 在 ASP.Net 中为不同的会话变量设置不同的超时

    是否可以为 ASP Net 中的不同会话设置不同的超时 Edited我的意思是 在同一页面中我有 2 个会话变量 Session ss1 和 Session ss2 是否可以为每个会话设置超时 或者是否有做同样的事情 比如将会话保存到 co
  • 将int所有字节设置为(unsigned char)0,保证代表零?

    This is not a matter of recommended practise nor undefined behavior but about what the c standard actually guarantees in
  • 如何添加一个范围内的数字

    嘿 所以我想做一个挑战 将 1000 以内 3 或 5 的倍数的每个数字相加 当我输入以下代码时 for x in xrange 1000 if x 3 0 or x 5 0 place list x sum place 它只是列出了所有
  • 是否可以解析整个字符串?

    如你所知 JavaScript 的parseFloat函数仅在遇到无效字符之前起作用 例如 parseFloat 10 123 10 123 parseFloat 12 zzzz 12 parseFloat z12 NaN 有没有一种方法或
  • ngx-bootstrap typeahead 不显示下拉菜单

    我正在将一个应用程序从 AngularJS 迁移到 Angular 但我在新的 typeahead 实现上遇到了障碍 已经过去一天了 我尝试了几个 API 最后决定采用最相似的 API我在 AngularJS 版本中使用的是什么 this
  • 英特尔处理器:“如果 CPUID.06H:EAX.[7] = 1”是什么意思?

    英特尔处理器数据表中的以下注释是什么意思 如何在 Linux 中的驱动程序代码中检查这一点 如果CPUID 06H EAX 7 1 我在英特尔处理器的软件开发人员手册的寄存器描述表的注释栏中遇到了这种类型的语句 Ref https soft
  • 如何在 Go 中管理 Windows 用户帐户?

    我需要能够从 Go 应用程序管理 Windows 本地用户帐户 并且似乎如果不使用 CGo 就没有本机绑定 我最初的搜索让我发现人们说最好使用 exec Command 来运行 net user 命令 但在解析响应代码时这似乎很混乱且不可靠
  • 实体框架 - MySQL - 日期时间格式问题

    我有一个简单的表格 其中有几个日期字段 每当我运行以下查询时 var docs from d in base EntityDataContext document reviews select d ToList 我收到以下异常 Unable
  • 带有 UIImage 或远程 URL 的 UNNotificationAttachment

    In my Notification Service Extension我正在从 URL 下载图像以显示为UNNotificationAttachment在通知中 因此 我将此图像作为 UIImage 并且不需要将其写入光盘上的应用程序目录
  • 使用 Google UMP SDK 时 Admob TCF 错误

    我最近根据以下要求集成了 UMP SKD文档 https developers google com admob ump android quick start 现在我从 Adbmob 收到错误报告 其中指出许多广告请求由于错误 1 1 而
  • Swift 3 中的 SCNGeometryElement 设置

    我硬着头皮开始将我的应用程序转换为 Swift 3 一如既往 该转换器还有很多不足之处 在这种情况下 我不确定如何正确编码新版本 这是原文 let indexes CInt 0 1 2 3 let dat NSData bytes inde
  • 为什么 dir 不显示所有 Python 对象属性?

    为什么当我调用时某些对象方法 属性不显示dir在物体上 Example from scipy import sparse e sparse eye 2 H in dir e returns False 但打电话e H工作得很好 返回另一个稀
  • 更改绑定属性的值时如何为文本块的背景设置动画?

    我有一个非常简单的 wpftoolkit da tagrid 来显示股票市场的出价和要价 我的网格绑定到ObservableCollection