【问题标题】:Flashing cell in GridView when it's updated by ObservableCollection当 GridView 被 ObservableCollection 更新时闪烁的单元格
【发布时间】:2020-12-11 23:38:08
【问题描述】:

我有一个: <controls:DataGrid> 在用 C# 制作的 UWP 开发中的 XAML 页面中,从 Web 服务更新价格。 通过 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);
    }
}

【问题讨论】:

  • 请问您是如何创建 DataGrid 并自动更新价格的?能否提供一些相关代码?
  • 谢谢,我更新了帖子添加了代码。
  • 当你得到最后一个更新的单元格时,你能告诉我们你想为它做什么以及如何刷写单元格吗?最好对绑定cell的模型进行操作,而不是直接获取cell。
  • 我想简单地改变一下背景颜色,恢复到原来的颜色。如果余额上升,则可能为绿色,如果余额下降,则可能为红色。谢谢

标签: c# uwp datagrid observablecollection uwp-xaml


【解决方案1】:

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

.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);

【讨论】:

  • Faywang 提出的解决方案,和我想要的非常接近,我按照 Faywang 所说的设置了颜色,然后Thread.Sleep(1000); 然后返回到以前的颜色。非常感谢Faywang!!!
猜你喜欢
  • 2017-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
  • 2015-03-18
  • 2020-07-11
相关资源
最近更新 更多