Skip to content

Commit d0dcdea

Browse files
committed
Add row background style selector feature.
1 parent 978df38 commit d0dcdea

File tree

3 files changed

+67
-21
lines changed

3 files changed

+67
-21
lines changed
16.9 KB
Loading

controls/datagrid/theming-and-styles/style-selectors.md

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,61 +11,107 @@ slug: datagrid-style-selectors
1111

1212
The [.NET MAUI DataGrid]({%slug datagrid-overview%}) component exposes a conditional styling feature. It allows users to apply different styles on a cell or per group header depending on a specific condition.
1313

14-
You can set a different style on a specific cell from a specific column based on custom selection logic with the following properties:
14+
## Cell Style Selector
15+
16+
You can set a different style on a specific cell from a specific column based on custom style-selection logic with the following properties:
1517

1618
* `CellContentStyleSelector`(`IStyleSelector`)—Styles the content of the cell by using the text alignment options (`TextMargin`, `HorizontalTextAlignment`, `VerticalTextAlignment`), the font options (`FontAttributes`, `FontFamily`, `FontSize`) and the `TextColor` property.
1719
* `CellDecorationStyleSelector`(`IStyleSelector`)—Styles the decoration of a cell.
1820

19-
Different styles can be applied on a per-group header once the DataGrid control is grouped through `GroupHeaderStyleSelector` property.
21+
> For the DataGrid Style Selector example, go to the [SDKBrowser Demo Application]({%slug sdkbrowser-app%}) and navigate to the **DataGrid > Styling** category.
22+
23+
## Row Background Style Selector
24+
25+
You can set a different style on a row, alternate row and on row details based on custom style-selection logic by using the `RowBackgroundStyleSelector` (`IStyleSelector`) property.
26+
27+
> For the DataGrid Row Background Style Selector example, go to the [SDKBrowser Demo Application]({%slug sdkbrowser-app%}) and navigate to the **DataGrid > Styling** category.
2028
21-
Different styles can be applied on a per-group footer once the DataGrid control is grouped through `GroupFooterStyleSelector` property.
29+
## Group Style Selector
30+
31+
You can set a different style on a group header and footer based on custom style-selection logic with the following properties:
32+
33+
* `GroupHeaderStyleSelector`(`IStyleSelector`)—Different styles can be applied on a per-group header once the DataGrid control is grouped
34+
* `GroupFooterStyleSelector`(`IStyleSelector`)—Different styles can be applied on a per-group footer once the DataGrid control is grouped
2235

2336
> To display the group footer, set the `ShowGroupFooters` property to `True`.
2437
25-
The `CellContentStyleSelector`, `CellDecorationStyleSelector`, and `GroupStyleSelector` use the `SelectStyle` method to change the style.
38+
> For the DataGrid Style Selector example, go to the [SDKBrowser Demo Application]({%slug sdkbrowser-app%}) and navigate to the **DataGrid > Styling** category.
2639
27-
## Example
40+
The `CellContentStyleSelector`, `CellDecorationStyleSelector`, `RowBackgroundStyleSelector`, `GroupHeaderStyleSelectorand`, and `GroupFooterStyleSelector` use the `SelectStyle` method to change the style.
2841

29-
The following example will demonstrate how to apply the style selectors in the DataGrid control:
42+
## Example with Cell and Group Style Selectors
3043

31-
Let’s add the DataGrid and set the `CellContentStyleSelector` as a static resource of type `MyCellContentStyleSelector`, `CellDecorationStyleSelector` as a static resource of type `MyCellDecorationStyleSelector`, and `GroupStyleSelector` as a static resource of type `MyGroupStyleSelector`.
44+
The following example will demonstrate how to apply the style selectors on the DataGrid cell and group header:
3245

33-
**1.** `DataGrid` definition in XAML:
46+
Let’s add the DataGrid and set the `CellContentStyleSelector` property as a static resource of type `MyCellContentStyleSelector`, `CellDecorationStyleSelector` as a static resource of type `MyCellDecorationStyleSelector`, and `GroupStyleSelector` as a static resource of type `MyGroupStyleSelector`.
47+
48+
**1.** Define the `RadDataGrid` in XAML:
3449

3550
<snippet id='datagrid-styleselector-example'/>
3651

37-
**2.** Create a simple data for the DataGrid:
52+
**2.** Create a sample data model:
3853

3954
<snippet id='datagrid-styleselector-data'/>
4055

41-
**3.** Set the `ItemsSource` of the `DataGrid` class:
56+
**3.** Set the `ItemsSource` of the `RadDataGrid`:
4257

4358
<snippet id='datagrid-styleselector-items'/>
4459

60+
**4.** Create a custom class for each selector. Each class derives from `IStyleSelector` and overrides its `SelectStyle` method.
61+
62+
* The implementation of `MyCellContentSelector` class:
4563

46-
**4.** Add `MyCellContentStyleSelector`, `MyCellDecorationStyleSelector`, and `MyGroupStyleSelector` as resources in the **Resource** page of the application:
64+
<snippet id='datagrid-styleselector-cellcontent'/>
65+
66+
* The implementation of `MyCellDecorationSelector` class:
67+
68+
<snippet id='datagrid-styleselector-celldecoration'/>
69+
70+
* The implementation of `MyGroupSelector` class:
71+
72+
<snippet id='datagrid-styleselector-group'/>
73+
74+
**5.** Add `MyCellContentSelector`, `MyCellDecorationSelector`, and `MyGroupSelector` as resources in the **Resource** page of the application:
4775

4876
<snippet id='datagrid-styleselectors'/>
4977

50-
**5.** Create a custom class for each selector. This class derives from `DataGridStyleSelector` and overrides its `SelectStyle` method.
78+
This is how the DataGrid control will look when `CellContentStyleSelector` is applied.
5179

52-
The `MyCellContentStyleSelector` class implementation is as follows:
80+
![.NET MAUI DataGrid Cell and Group Header Style Selectors](../images/datagrid-style-selector.png)
5381

54-
<snippet id='datagrid-styleselector-cellcontent'/>
82+
> For the DataGrid Style Selector example, go to the [SDKBrowser Demo Application]({%slug sdkbrowser-app%}) and navigate to the **DataGrid > Styling** category.
5583
56-
The implementation of the `MyCellDecorationStyleSelector` class is shown below:
84+
## Example with Row Background Style Selectors
5785

58-
<snippet id='datagrid-styleselector-celldecoration'/>
86+
The following example will demonstrate how to apply the style selectors on the DataGrid rows, row details and alternate rows:
5987

88+
Let’s add the DataGrid and set the `RowBackgroundStyleSelector` property as a static resource of type `MyRowBackgroundStyleSelector`.
6089

61-
You can implement `MyGroupStyleSelector` as follows:
90+
**1.** Define the `RadDataGrid` in XAML:
6291

63-
<snippet id='datagrid-styleselector-group'/>
92+
<snippet id='datagrid-rowbackground-styleselector-example'/>
6493

94+
**2.** Create a sample data model:
6595

66-
This is how the DataGrid control will look when `CellContentStyleSelector` is applied.
96+
<snippet id='datagrid-rowbackground-styleselector-model'/>
97+
98+
**3.** Define the `ViewModel`:
99+
100+
<snippet id='datagrid-rowbackground-styleselector-viewmodel'/>
101+
102+
**4.** Create a custom class `MyRowBackgroundStyleSelector` that derives from `IStyleSelector` and override the `SelectStyle` method.
103+
104+
<snippet id='datagrid-rowbackground-styleselector-class'/>
105+
106+
**5.** Add `MyRowBackgroundStyleSelector` as resource in the **Resource** page of the application:
107+
108+
<snippet id='datagrid-rowbackground-styleselector'/>
109+
110+
This is how the DataGrid control looks when applying the `RowBackgroundStyleSelector`.
111+
112+
![.NET MAUI DataGrid Row Background Style Selector](../images/datagrid-rowbackground-style-selector.png)
67113

68-
![DataGrid StyleSelectors](../images/datagrid-style-selector.png)
114+
> For the DataGrid Row Background Style Selector example, go to the [SDKBrowser Demo Application]({%slug sdkbrowser-app%}) and navigate to the **DataGrid > Styling** category.
69115
70116
## See Also
71117

controls/datagrid/theming-and-styles/styling.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The next image shows the end result.
3535
You can style the rows and cells of the DataGrid with the following properties:
3636

3737
* `RowBackgroundStyle`(of type `Style` with target type `DataGridRowBackgroundAppearance`)&mdash;Defines the style of each row.
38-
* `AlternateRowBackgroundStyle`(of type `Style` with target type `DataGridAlternateRowBackgroundAppearance`)&mdash;Defines the appearance style of an alternated row.
38+
* `AlternateRowBackgroundStyle`(of type `Style` with target type `DataGridRowBackgroundAppearance`)&mdash;Defines the appearance style of an alternated row.
3939
* `SelectionStyle`(of type `Style` with target type `DataGridSelectionAppearance`)&mdash;Defines the appearance settings applied to the selected DataGrid row.
4040
* `CurrentCellStyle`(of type `Style` with target type `DataGridCurrentCellAppearance`)&mdash;Defines the style applied to the current cell.
4141
* (Desktop-only)`MouseHoverStyle`(of type `Style` with target type `DataGridMouseHoverAppearance`)&mdash;Specifies the style applied to the cells and rows when the mouse is over them.

0 commit comments

Comments
 (0)