XamDataGrid has a property HighLightingAlternateRecords which has to be set to true if we need alternating rows highlighted, but when a XamDataGrid is used to display treeview setting this property does not show alternating rows highlighted. This article shows an alternative way to display the same.
The XamDataGrid has two events XamDataGrid_RecordCollapsed and XamDataGrid_RecordExpanded, which we can use to achieve this functionality.
First of all we need to display a treeView in XamdataGrid, to do the same we can follow the step mentioned in the below link
http://community.infragistics.com/forums/p/16974/213729.aspx#213729
So now I assume that we have a class (Node) defined which contains the properties required to display the TreeView nodes and we are going to bind an ObservableCollection<Node> to the XamDataGrid.DataSource as shown in the above link.
To get the alternate rows highlighting we need to add one more property to the class Node called as IsVisuallyExpanded to know if the tree node is expanded to show all its child items.
We also need to get the list of items that are bound to the XamDataGrid in a linear fashion without the tree structure and by using the IsVisuallyExpanded property that we added, we can get only those nodes which are currently visible in the tree view (excluding the child items which are collapsed).
When the tree node is collapsed, we first find which node was collapsed and for this node and all its child nodes we set the IsVisuallyExpaned property to false as shown below.
void XamDataGrid1_RecordCollapsed(object sender, RecordCollapsedEventArgs e) {
var treeNodeThatWasCollapsed = ((e.Record as DataRecord).DataItem as Node);
foreach (var child in treeNodeThatWasCollapsed.ChildNodes) {
child.IsVisuallyExpanded = false;
}
SetBackgroundColor();
}
Similarly when the tree node is expanded, we again find the node which was expanded and set the IsVisuallyExpanaded property to true for both the node as well as all its child nodes as show below.
void XamDataGrid1_RecordExpanded(object sender, RecordExpandedEventArgs e) {
var treeNodeThatWasExpanded = ((e.Record as DataRecord).DataItem as Node);
treeNodeThatWasExpanded.IsVisuallyExpanded = true;
foreach (var child in treeNodeThatWasExpanded.ChildNodes) {
child.IsVisuallyExpanded = true;
}
SetBackgroundColor();
}
Then in each of these events we call a function which will first clear the background color for all the nodes and then loop through only the currently visible records and change the color accordingly for alternate rows as shown below in SetBackgroundColor() method.
void SetBackgroundColor() {
bool isColored = true;
var backColor = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(231, 241, 255));
if (_model != null) {
this._model.FlatListOfVisibleNodes.ForEach(x => x.BackgroundColor = System.Windows.Media.Brushes.Transparent);
foreach (var rec in this._model.FlatListOfVisibleNodes) {
if (isColored) {
rec.BackgroundColor = backColor;
}
isColored = !isColored;
}
}
}

We can also see the different threads being called for execution in the Thread window. We can see the thread window by clicking Debug/windows/Thread. 







