I'm currently doing a first project with xamDataGrid. I have a hierarchical data set bound to a xamDataGrid. When I expand a record, the child record look the same as the parent records, which makes the screen a bit unclear (not so easy to distinguish between parent and child records at first view).
How can I change background color, foreground color and font for the child records?
Thanks in advance for any help.
Hello,
You can set these properties in a Style for the DataRecordCellArea of the FieldLayout:
<Grid> <Grid.Resources> <Style x:Key="style" TargetType="{x:Type igDP:DataRecordCellArea}"> <Setter Property="Foreground" Value="Red"/> <Setter Property="Background" Value="Azure"/> <Setter Property="FontFamily" Value="Comic Sans MS"/> </Style> </Grid.Resources> <igDP:XamDataGrid Name="grid"> <igDP:XamDataGrid.FieldLayouts> <igDP:FieldLayout Key="Parent"> <igDP:FieldLayout.Fields> <igDP:Field Name="CustomerID"/> <igDP:Field Name="FirstName"/> <igDP:Field Name="LastName"/> <igDP:Field Name="DateJoined"/> <igDP:Field Name="IsActive"/> <igDP:Field Name="Orders"/> </igDP:FieldLayout.Fields> </igDP:FieldLayout> <igDP:FieldLayout Key="Child"> <igDP:FieldLayout.Settings> <igDP:FieldLayoutSettings DataRecordCellAreaStyle="{StaticResource style}"/> </igDP:FieldLayout.Settings> <igDP:FieldLayout.Fields> <igDP:Field Name="OrderID"/> <igDP:Field Name="CustomerID"/> <igDP:Field Name="OrderDate"/> </igDP:FieldLayout.Fields> </igDP:FieldLayout> </igDP:XamDataGrid.FieldLayouts> </igDP:XamDataGrid> </Grid>
Hi Aaron
Thanks for the answer. I see in your code how you do it using Xaml. In my case, the data comes from a database and there are no FieldLayout definitions in my Xaml code.
How can I do this in code?
You can create the style in code behind like this:Style style = new Style(typeof(DataRecordCellArea));
Setter s1 = new Setter(DataRecordCellArea.BackgroundProperty, Brushes.Red);
Setter s2 = new Setter(DataRecordCellArea.ForegroundProperty, BrushesBlack);
...
style.Setters.Add(s1);
style.Setters.Add(s2);
and then set the Style. For example:
xamDataGrid1.SetValue(DataRecordCellArea.StyleProperty,style;
Alex.
Hi Alex
Thanks for the code. I've tried this out and have encountered two problems:
1) I get the following error message when the SetValue(.) statement is executed:
An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dllAdditional information: 'DataRecordCellArea' TargetType does not match type of element 'XamDataGrid'.
It seems that the value can not be applied to the xamDataGrid.
2) The code to set the style as suggested seems to change the appearance of all rows. I only want to change the appearance of the child rows. So I need to know how to distinguish between the different hierarchy levels and how to change the appearance according to the hierarchy level.
Any futher ideas?
Hi,
What you are asking is FieldLayouts. There is collection in the xamDataGrid with the FieldLayouts. For example
FieldLayouts[0] - parent, first level;
FieldLayouts[1] - child, second level;
... etc.
Then get the DataRecordCellArea, CellValuePresenter, DataRecordPresenter and apply the style.