My app as it initiates it populates a DataGrid with data retrieved from a Data Base. My fields are: Name, ID and Status. The first two no problem, but the third one (Status) is an int, either 1 or 0, indicating active or inactive.
I decided to create an User Control to show the “Status” field. It’s nothing more than a circle wich turns green or red depending on the value of “Status”. The default color of the control is red.
I also created a list object(“data”) that contains my information plus the control with it’s respective color. So far no problem. Everything works, and if I do a quick watch on my data object, everything is as it should be.
My problem is that after I populate the DataGrid with “myDataGrid.ItemsSource = data”, every control “Status” appears as red, when some of them should be green. If I breakpoint at any given time and quick watch my “data” object, everything is fine, but the DataGrid does not update.
The same happens for sorting. If I change the color of any of the controls, after sorting any of the columns, it resets the color of all my user controls to it’s default (red).
After a few tests and research I think that the problem is that the DataGrid updates itself at the end of the item source and instanciates new User Controls instead of using the ones on my data object.
Is there anyway to solve this? I’ve tried a few solutions that I’ve found around the web and nothing worked so far!
Lets assume your class looks something like this…
Is my assumptions correct?
If so, I have a few recommendations
UserControltype (i.e.StatusControl) property from your model (i.e.MyObjectclass)Do not use
AutogenerateColumnsas true (which is by default true) on the datagrid. Create your specific columns…Here your user control (like the
StatusControl) should expose aBrushproperty (something likeIndicatorColor) so that based on the Status, theFillcolor of the Circle in the user control (see theDataTrigger) will change.So this way as you receive updates on
Statusproperty ofMyObjecttheDataTriggerwill update the color on the user control.Hope this helps…