Reputation: 53
I have silverlight application that each 1 second changes the observable collection but what happend is that after you sort the sort is canceled. how can i solve it? this happens each 1 second:
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());
}
Upvotes: 2
Views: 48
Reputation: 2782
As I can see you reset(create a new observable collection) your collection each timer tick, thus in my opinion, the sort description of a DataGrid's ItemsSource is cleared. I think that if will replace your re-creating(creating the new observable collection) code with the next one, it will help you to preserve the origin sort description.
A new ClockTimerOnTick method code
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"
});
});
}
Here we just clear and re-fill the LongTermModel.Open collection each timer tick.
Regards.
Upvotes: 1