【发布时间】: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