Skip to content

Commit d644837

Browse files
Merge pull request #1251 from telerik/new-kb-conditionally-show-hide-expand-arrow-datagrid-1bf46cc47dc64318938659384e6f74b0
Added new kb article conditionally-show-hide-expand-arrow-datagrid
2 parents 5cd0ab3 + db31642 commit d644837

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
title: Conditionally Showing or Hiding the Expand Arrow in Toggle Row Details Column Cells
3+
description: Learn how to conditionally show or hide the expand arrow in the toggle row details column cells in the DataGrid for UI for .NET MAUI when there are no details for the rows.
4+
type: how-to
5+
page_title: Conditionally Display Expand Arrow in DataGrid Toggle Row Details Column
6+
meta_title: Conditionally Display Expand Arrow in DataGrid Toggle Row Details Column
7+
slug: conditionally-show-hide-expand-arrow-datagrid
8+
tags: datagrid, ui-for-net-maui, toggle-row-details-column, isvisible-property, cellcontentstyleselector
9+
res_type: kb
10+
ticketid: 1700211
11+
---
12+
13+
## Environment
14+
15+
| Version | Product | Author |
16+
| --- | --- | ---- |
17+
| 11.1.0 | Telerik UI for .NET MAUI DataGrid | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova)|
18+
19+
## Description
20+
21+
I want to conditionally hide the expand arrow in the toggle row details column cells when there are no details for the rows in the DataGrid for UI for .NET MAUI.
22+
23+
This knowledge base article also answers the following questions:
24+
- How to hide the expand arrow in DataGrid for UI for .NET MAUI when no row details exist?
25+
- How to implement conditional visibility for the toggle row details button?
26+
- How to stop default command execution for empty row details?
27+
28+
## Solution
29+
30+
To hide the expand arrow in the toggle row details column cells when no row details exist, follow these steps:
31+
32+
1. Implement a custom style selector to apply different styles based on whether the row has details.
33+
34+
```csharp
35+
public class RowDetailsColumnStyleSelector : IStyleSelector
36+
{
37+
public Style Style1 { get; set; }
38+
public Style Style2 { get; set; }
39+
40+
public Style SelectStyle(object item, BindableObject bindable)
41+
{
42+
var dataItem = (DataGridCellInfo)item;
43+
if (dataItem.Item is Data data)
44+
{
45+
if (string.IsNullOrEmpty(data.Details))
46+
{
47+
return this.Style1; // Apply transparent button style when no details exist.
48+
}
49+
}
50+
return this.Style2; // Apply default button style.
51+
}
52+
}
53+
```
54+
55+
2. Define styles for the toggle row details column cells in your XAML.
56+
57+
```xml
58+
<ContentPage.Resources>
59+
<ResourceDictionary>
60+
<!-- Define RowDetailsCellStyleSelector -->
61+
<local:RowDetailsColumnStyleSelector x:Key="RowDetailsCellStyleSelector">
62+
<local:RowDetailsColumnStyleSelector.Style1>
63+
<Style TargetType="telerik:DataGridToggleRowDetailsCellAppearance">
64+
<Setter Property="ButtonTextColor" Value="Transparent" />
65+
<Setter Property="ButtonFontSize" Value="0" />
66+
</Style>
67+
</local:RowDetailsColumnStyleSelector.Style1>
68+
<local:RowDetailsColumnStyleSelector.Style2>
69+
<Style TargetType="telerik:DataGridToggleRowDetailsCellAppearance">
70+
<Setter Property="ButtonTextColor" Value="Black" />
71+
</Style>
72+
</local:RowDetailsColumnStyleSelector.Style2>
73+
</local:RowDetailsColumnStyleSelector>
74+
75+
<!-- Apply CellContentStyleSelector to ToggleRowDetailsColumn -->
76+
<Style TargetType="telerik:DataGridToggleRowDetailsColumn">
77+
<Setter Property="CellContentStyleSelector" Value="{StaticResource RowDetailsCellStyleSelector}" />
78+
</Style>
79+
</ResourceDictionary>
80+
</ContentPage.Resources>
81+
```
82+
83+
3. Implement a custom `ToggleRowDetailsButtonTap` command to prevent the default behavior when row details are empty.
84+
85+
```csharp
86+
public class MyToggleRowDetailsButtonTap : DataGridCommand
87+
{
88+
public MyToggleRowDetailsButtonTap()
89+
{
90+
Id = DataGridCommandId.ToggleRowDetailsButtonTap;
91+
}
92+
93+
public override bool CanExecute(object parameter)
94+
{
95+
return true;
96+
}
97+
98+
public override void Execute(object parameter)
99+
{
100+
var data = parameter as Data;
101+
if (string.IsNullOrEmpty(data?.Details))
102+
{
103+
return; // Prevent default execution when no row details exist.
104+
}
105+
this.Owner.CommandService.ExecuteDefaultCommand(DataGridCommandId.ToggleRowDetailsButtonTap, parameter);
106+
}
107+
}
108+
```
109+
110+
4. Set up the `RadDataGrid` and link the resources.
111+
112+
```xml
113+
<telerik:RadDataGrid x:Name="dataGrid"
114+
AutoGenerateColumns="False"
115+
RowDetailsTemplate="{StaticResource TemplateForRowDetails}">
116+
<telerik:RadDataGrid.Columns>
117+
<telerik:DataGridToggleRowDetailsColumn CanUserReorder="False"/>
118+
<telerik:DataGridTextColumn PropertyName="Country" />
119+
<telerik:DataGridTextColumn PropertyName="Capital" />
120+
</telerik:RadDataGrid.Columns>
121+
</telerik:RadDataGrid>
122+
```
123+
124+
In the code behind, assign the `ItemsSource` and add the custom command.
125+
126+
```csharp
127+
List<Data> items = new List<Data>
128+
{
129+
new Data { Country = "India", Capital = "New Delhi", Details = "Details about India." },
130+
new Data { Country = "South Africa", Capital = "Cape Town", Details = "Details about South Africa." },
131+
new Data { Country = "Nigeria", Capital = "Abuja", Details = "Details about Nigeria." },
132+
new Data { Country = "Singapore", Capital = "Singapore" }
133+
};
134+
135+
this.dataGrid.ItemsSource = items;
136+
this.dataGrid.Commands.Add(new MyToggleRowDetailsButtonTap());
137+
```
138+
139+
## See Also
140+
141+
- [DataGrid Overview](https://www.telerik.com/maui-ui/documentation/controls/datagrid/overview)
142+
- [CellContentStyleSelector Documentation](https://www.telerik.com/maui-ui/documentation/controls/datagrid/theming-and-styles/style-selectors)

0 commit comments

Comments
 (0)