I'm trying to use the datagrid loadingrow event to color my row based on the value within the row. I have the following code within the loadingrow event handler:
DataRowView item = e.Row.Item as DataRowView;
if (item != null)
{
// my code to set the row background color
}

My problem is that the variable item is always null so my code never gets executed. I've used the debugger and when I view the "e.Row.Item" value, it does indeed show something besides "null." So, I'm at a loss for what would cause the assignment to result in "null" instead of the value I see as "e.Row.Item."
Can anyone shed any light on the problem and offer a solution?
I've inserted a "Console.WriteLine(e.Row.Item);" statement right after the above "if" statement and it indeed displays a value, not null!
Thanks in advance for the help.

I don't believe that to be the case because eventually, the code would be executed at some point in time. I've placed a breakpoint inside the "if" block and the breakpoint is never reached...also, I've scrolled the data to force the datagrid to refresh itself and it still doesn't enter my "if" block...

3 solutions

Solution 3

So, one more update to this puppy just to let folks know about new information that I found. In my LoadingRow event handler previously, I was setting the variable like this:
DataRowView item = e.Row.Item as DataRowView;

and then checking it's value, only to find that the value of item was never getting updated...
I did some more searching on the internet and found another "solution" that works in the LoadingRow event handler (in addition to the XAML trigger solution I previously posted). So, here's the "new" LoadingRow event handler code:

Note the difference in the new code is that I create a RowDataContext variable rather than a DataRowView variable. The resulting code is a bit simpler and IT WORKS! The DataGrid is bound to an observablecollection of "OptionChain" objects and ochgdir is a property of each of those objects.
I'm wondering if the problem with the original version might have something to do with one-way vs. two-way binding which, in effect, doesn't allow the assignment in my original LoadingRow event handler? If that's not the case, oh well, it's still a mystery!
Thanks.

Solution 1

The problem occurs probably because the LoadingRow event may actually fire several times. The Datagrid will only instantiate the row when it is needed and will recycle it when it goes out of view. Like when you start scrolling the grid.

Solution 2

Well, I didn't actually solve the problem of why the assignment wasn't happening in my LoadingRow event handler, that's still a MYSTERY; but I did accomplish what I wanted another way. I modified my XAML to include the following code:
<pre lang="xml"><DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="TextElement.Foreground" Value="Green"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ochgdir}" Value="Down">
<Setter Property="TextElement.Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle></pre>

The result is that when the "ochgdir" property of the object bound to the DataGridRow contains the characters "Down" then the foreground color of the DataGrid row data is set to "Red" otherwise it's set to "Green."
Wonderful aspect of "programming" is that there's always more than one solution to a problem...it's just a matter of finding one that works for you! I DID!
Thanks to those who've pondered my post and to Ganesan for the reply.