From 74636a696b5dfb63c5f1c5591dcb9714a5260f80 Mon Sep 17 00:00:00 2001 From: Edward Miller Date: Sun, 20 Oct 2024 19:09:03 -0500 Subject: [PATCH] add placeholder space when column has filtering disabled --- Maui.DataGrid/DataGridColumn.cs | 4 ++++ Maui.DataGrid/DataGridHeaderRow.cs | 37 +++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/Maui.DataGrid/DataGridColumn.cs b/Maui.DataGrid/DataGridColumn.cs index 241f0ed..2fbcd91 100644 --- a/Maui.DataGrid/DataGridColumn.cs +++ b/Maui.DataGrid/DataGridColumn.cs @@ -211,6 +211,8 @@ public DataGridColumn() VerticalOptions = LayoutOptions.Center, }; + FilterTextboxContainer = new ContentView(); + FilterTextbox.SetBinding(Entry.TextProperty, new Binding(nameof(FilterText), BindingMode.TwoWay, source: this)); } @@ -406,6 +408,8 @@ public Style HeaderFilterStyle internal Label HeaderLabel { get; } = new(); + internal ContentView FilterTextboxContainer { get; } + internal View SortingIconContainer { get; } internal SortingOrder SortingOrder { get; set; } diff --git a/Maui.DataGrid/DataGridHeaderRow.cs b/Maui.DataGrid/DataGridHeaderRow.cs index 3d21206..7890bcb 100644 --- a/Maui.DataGrid/DataGridHeaderRow.cs +++ b/Maui.DataGrid/DataGridHeaderRow.cs @@ -179,11 +179,10 @@ private void OnVisibilityChanged(object? sender, EventArgs e) private DataGridCell CreateHeaderCell(DataGridColumn column) { - var filteringEnabled = DataGrid.FilteringEnabled && column.FilteringEnabled; - if (column.HeaderCell != null) { - column.FilterTextbox.IsVisible = filteringEnabled; + SetFilterRow(column); + return column.HeaderCell; } @@ -214,15 +213,37 @@ private DataGridCell CreateHeaderCell(DataGridColumn column) CommandParameter = column, }); - /* Configure the filter textbox */ + SetFilterRow(column); - column.FilterTextbox.IsVisible = filteringEnabled; - cellContent.Children.Add(column.FilterTextbox); - cellContent.SetRow(column.FilterTextbox, 1); - cellContent.SetColumnSpan(column.FilterTextbox, 2); + cellContent.Children.Add(column.FilterTextboxContainer); + cellContent.SetRow(column.FilterTextboxContainer, 1); + cellContent.SetColumnSpan(column.FilterTextboxContainer, 2); return new DataGridCell(cellContent, DataGrid.HeaderBackground, column, false); } + private void SetFilterRow(DataGridColumn column) + { + if (DataGrid.FilteringEnabled && column.FilteringEnabled) + { + column.FilterTextboxContainer.Content = column.FilterTextbox; + } + else if (DataGrid.FilteringEnabled && DataGrid.Columns.Any(c => c.FilteringEnabled)) + { + var height = DataGrid.Columns.First(c => c.FilteringEnabled).FilterTextbox.Height; + + // Add placeholder + column.FilterTextboxContainer.Content = new BoxView + { + MinimumHeightRequest = height, + Color = Colors.Transparent, + }; + } + else + { + column.FilterTextboxContainer.Content = null; + } + } + #endregion Methods }