ObservableCollection 更新时在 GridView 中闪烁单元格

Flashing cell in GridView when it's updated by ObservableCollection

我有一个: <controls:DataGrid> 在用 C# 开发的 UWP 开发中的 XAML 页面中,从网络服务更新价格。 通过 ObservableCollection,单元格自动更新价格。 我希望单元格在更新其值时闪烁。

我不知道如何通过 ObservableCollection 元素获取 GridView 的最后更新单元格,以及它在以编程方式更新时引发的事件。

XAML部分:

<controls:DataGrid  x:Name="dgBalances" Height="600" Margin="12" AutoGenerateColumns="True" ItemsSource="{x:Bind Info.Balances }" Grid.Row="4" Grid.Column="0" AlternatingRowBackground="SlateGray" RowBackground="DimGray"/>

函数部分:

private async Task UpdateBalancesServiceAsync(ObservableCollection<AssetBalance> balances, MyWebservice client)
{
    //Run like service updating the observablecollection
    while (true)
    {
        // Get All Balances
        var ret = await client.GetBalancesAsync();
        if (ret.Success)
        {
            foreach (var returnedbalance in ret.Data)
            {
                
                if (balances.FirstOrDefault(x => x.Currency.Equals(returnedbalance.Currency)) != null)
                {
                    // If Balance exists and the Value needs update.
                    if (balances.FirstOrDefault(x => x.Currency.Equals(returnedbalance.Currency)).Value != returnedbalance.Total)
                    {
                        _ = CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                            () =>
                            {
                                balances.FirstOrDefault(x => x.Currency.Equals(returnedbalance.Currency)).Value = returnedbalance.Total;
                                balances.FirstOrDefault(x => x.Currency.Equals(returnedbalance.Currency)).LastUpdate = DateTime.Now;
                            });
                    }
                    else
                    {
                        // If Balance exists and the Value doesn't need update, just show the time last checked.
                        _ = CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                            () =>
                            {
                                balances.FirstOrDefault(x => x.Currency.Equals(returnedbalance.Currency)).LastUpdate = DateTime.Now;
                            });
                    }
                        
                }
                else
                {
                    // If Balance does not exists it will create the new one.
                    _ = CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                        () =>
                        {
                            balances.Add(new AssetBalance()
                            {
                                LastUpdate = DateTime.Now,
                                Currency = returnedbalance.Currency,
                                Value = returnedbalance.Total
                            });
                        });
                
                }
            }
        }
        Thread.Sleep(10000);
    }
}

您可以尝试自定义单元格以创建您自己的列类型,方法是指定用于显示的单元格模板,然后声明背景色 属性 以与您要闪烁的列绑定。例如,我创建了一个货币列,当余额达到时,更改其背景。

.xaml:

<controls:DataGrid Height="600" Margin="12" AutoGenerateColumns="False" ItemsSource="{x:Bind Balances,Mode=OneWay}" Grid.Row="4" Grid.Column="0" AlternatingRowBackground="SlateGray" RowBackground="DimGray">
    <controls:DataGrid.Columns>
        <!--Currency Column-->
        <controls:DataGridTemplateColumn Header="Currency">
            <controls:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel Background="{Binding CellBackground}" HorizontalAlignment="Stretch">
                        <TextBlock Text="{Binding Currency}"/>
                    </StackPanel>
                </DataTemplate>
            </controls:DataGridTemplateColumn.CellTemplate>
        </controls:DataGridTemplateColumn>
        ......
    </controls:DataGrid.Columns>
</controls:DataGrid>

.cs:

public class AssetBalance : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    ......
    public SolidColorBrush cellBackground { get; set; }
    public SolidColorBrush CellBackground
    {
        get { return cellBackground; }
        set
        {
            cellBackground = value;
            this.OnPropertyChanged();
        }
    }

    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

//如果blance增加,改变背景

AssetBalance blance = Balances[index];
blance.CellBackground = new SolidColorBrush(Colors.Red);