更改数据源后取消排序
Sorting is canceled after change datasource
我有一个 silverlight 应用程序,它每 1 秒更改一次可观察集合,但发生的事情是,在您排序后,排序被取消了。
我该如何解决?
这每 1 秒发生一次:
private async void ClockTimerOnTick(object sender, EventArgs eventArgs)
{
var allOpenTrades = await NewAPI.GetOpenTrades();
var openLongTrades = allOpenTrades.Where(x => x.gameType == (int)GameType.LongTerm);
LongTermModel.Open = new ObservableCollection<OpenTranasctionLongTerm>
(openLongTrades.Select(x => new OpenTranasctionLongTerm
{
isPut = x.CallPutStatusId == 2,
DateTraded = x.TransactionCreatedOn.ToLocalTime(),
Expiration = x.optionExpirationTime.ToLocalTime(),
Payout = x.OptionWinReturn,
Security = x.OptionName,
StrikePrice = x.TransactionQuote,
Traded = x.Amount,
Currency = UserCurrency,
isCall = x.CallPutStatusId == 1,
Type = x.CallPutStatusId == 1 ? "Call" : "Put"
}).ToList());
}
正如我所看到的,您在每次计时器滴答时重置(创建一个新的可观察集合)您的集合,因此在我看来,DataGrid 的 ItemsSource 的 sort description 被清除。我认为如果将您的重新创建(创建新的可观察集合)代码替换为下一个代码,它将帮助您保留原始排序描述。
新的 ClockTimerOnTick 方法代码
private void ClockTimerOnTick(object sender, EventArgs eventArgs)
{
var allOpenTrades = NewAPI.GetOpenTrades();
var openLongTrades = allOpenTrades.Where(x => x.gameType == (int)GameType.LongTerm).ToList();
//I'm assuming here that the LongTermModel.Open is an observable collection
LongTermModel.Open.Clear();
openLongTrades.ForEach(term =>
{
LongTermModel.Open.Add(new OpenTranasctionLongTerm
{
isPut = x.CallPutStatusId == 2,
DateTraded = x.TransactionCreatedOn.ToLocalTime(),
Expiration = x.optionExpirationTime.ToLocalTime(),
Payout = x.OptionWinReturn,
Security = x.OptionName,
StrikePrice = x.TransactionQuote,
Traded = x.Amount,
Currency = UserCurrency,
isCall = x.CallPutStatusId == 1,
Type = x.CallPutStatusId == 1 ? "Call" : "Put"
});
});
}
这里我们只是在每个计时器滴答时清除并重新填充 LongTermModel.Open 集合。
此致。
我有一个 silverlight 应用程序,它每 1 秒更改一次可观察集合,但发生的事情是,在您排序后,排序被取消了。 我该如何解决? 这每 1 秒发生一次:
private async void ClockTimerOnTick(object sender, EventArgs eventArgs)
{
var allOpenTrades = await NewAPI.GetOpenTrades();
var openLongTrades = allOpenTrades.Where(x => x.gameType == (int)GameType.LongTerm);
LongTermModel.Open = new ObservableCollection<OpenTranasctionLongTerm>
(openLongTrades.Select(x => new OpenTranasctionLongTerm
{
isPut = x.CallPutStatusId == 2,
DateTraded = x.TransactionCreatedOn.ToLocalTime(),
Expiration = x.optionExpirationTime.ToLocalTime(),
Payout = x.OptionWinReturn,
Security = x.OptionName,
StrikePrice = x.TransactionQuote,
Traded = x.Amount,
Currency = UserCurrency,
isCall = x.CallPutStatusId == 1,
Type = x.CallPutStatusId == 1 ? "Call" : "Put"
}).ToList());
}
正如我所看到的,您在每次计时器滴答时重置(创建一个新的可观察集合)您的集合,因此在我看来,DataGrid 的 ItemsSource 的 sort description 被清除。我认为如果将您的重新创建(创建新的可观察集合)代码替换为下一个代码,它将帮助您保留原始排序描述。
新的 ClockTimerOnTick 方法代码
private void ClockTimerOnTick(object sender, EventArgs eventArgs)
{
var allOpenTrades = NewAPI.GetOpenTrades();
var openLongTrades = allOpenTrades.Where(x => x.gameType == (int)GameType.LongTerm).ToList();
//I'm assuming here that the LongTermModel.Open is an observable collection
LongTermModel.Open.Clear();
openLongTrades.ForEach(term =>
{
LongTermModel.Open.Add(new OpenTranasctionLongTerm
{
isPut = x.CallPutStatusId == 2,
DateTraded = x.TransactionCreatedOn.ToLocalTime(),
Expiration = x.optionExpirationTime.ToLocalTime(),
Payout = x.OptionWinReturn,
Security = x.OptionName,
StrikePrice = x.TransactionQuote,
Traded = x.Amount,
Currency = UserCurrency,
isCall = x.CallPutStatusId == 1,
Type = x.CallPutStatusId == 1 ? "Call" : "Put"
});
});
}
这里我们只是在每个计时器滴答时清除并重新填充 LongTermModel.Open 集合。
此致。