"....The CellControlAttached event not only fires when a cell is brought into view, but it also fires when a cell binding is refreshed due to an underlying change in the datasource...."
Within the event how do we know, if the event was fired due a change in the underlying datasource or it was just brought into view (by switching between screens etc..)?
Pls. advice.
There isn't a way to tell, but why do you need to know this? Could you tell me your use case?
We want to change the background color of the cell only during binding datasource refresh. (to highlight realtime updates)..
currently i'm setting the style in the cellcontrolattachedevent.. the problem is the color changes not only during datasource changes... but also when the user switches to another screen and then comesback to the original screen...
The CellControlAttached is only going to fire for cells that are in view that have a data update. So if your cell was scrolled out of view at the time, even if the value changed you wouldn't be notified through the event.
You could use the ConditionalFormatting feature in the grid to do this for you. Otherwise if you are committed to doing the styling yourself you would need to do some tracking.
As a suggestion you could use the .Tag value of the cell to record the value of the cell in question and if it refires check the value to see its changed since the last time you checked the value.
so something like this in your CellControlledAttaced
for this psudocode I am assuming you are only tracking a single column (called DollarValue) off an object typed MyObject
if e.Cell.Column.Key == DollarValue
MyObject myObj = e.Cell.Row.Data as MyObject;
if e.Cell.Tag == null // this cell has never been rendered
e.Cell.Tag = myObj.DollarValue;
else // we have rendered this cell before
{
double exitingValue = (double) e.Cell.Tag; // get the existing vlaue out of tag, DollarValue is a double
if (existingValue != myObj.DollarValue)
// do styling
}
With the above code, the problem we have is not totally eliminated.
The issue is, if a cell was already rendered and then taken out of view and the datasource changed when the cell was out of view, and later brought back into view....
At this point, the CellControlAttached event fires but within the event (the e.Cell.Tag will not be null now) we still cannot determine if the datasource refresh triggered this event..
If there was a CellControlDetached event then we could have possibly set the e.Cell.tag to null again which would help us.
If you changed your datasource all the Cells should destroyed and recreated, so if you have the Tag value set then the datasource hasn't changed, just came and gone from view.
Earlier when i mentioned that the "datasource changed", I wasn't referring to the entire grid. I was just referring to the value of a particular cell that was changed....in which case the Cell.Tag will not be null right?
In this case I would say you should probably be handling this on the data level in your data object itself by recording meta data about your data. the XamGrid isn't going to know what happens off screen, by design.
I was trying to figure out a mechanism that would work for you, but in almost every case there would be an issue.
You could scroll thorugh all your rows and cells after binding and "pre-Tag" all your values. This assumes that rows won't be added to the datasource. so that is one limitation. The second would be that if off screen a value goes from 10 to 100 to 11, how would you expect the coloration to work? With this mechansim you would only possibly see the last transition with respect to the init value, so you would see a +1 rahter then the -89.
If it's possible I would say record metadata on your data object regarding your data transitions
something like
double MyData
get; // put code here
set
if (value != this._myData)
if _myData < value
_myDataLastTransition = MyNewTransitionEnum.ValueIncreased;
else
_myDataLastTransition = MyNewTransiitionEnum.ValueDecreased
MyNewTransitionEnum MyDataLastTransion {get {return this._myDataLastTransition}
You would probaby want to add INotifyPropertyChanged notifiers on both fields as well, but the principle would be that when your real data is changed (MyData = newValue) then the dataobject would record the last transitions direction. And then you could base your decisions on that.