Skip to content

Commit

Permalink
initialize column data type if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Miller committed Dec 29, 2023
1 parent e638025 commit e75a119
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Maui.DataGrid/DataGrid.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,8 @@ private void InitHeaderView()
{
var col = Columns[i];

col.InitializeDataType();

col.ColumnDefinition ??= new(col.Width);

col.DataGrid ??= this;
Expand Down
23 changes: 15 additions & 8 deletions Maui.DataGrid/DataGridColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ public event EventHandler SizeChanged

internal SortingOrder SortingOrder { get; set; }

internal Type? DataType { get; private set; }

internal DataGrid? DataGrid { get; set; }

internal ColumnDefinition? ColumnDefinition
Expand Down Expand Up @@ -338,15 +340,9 @@ public bool IsSortable(DataGrid dataGrid)
{
_isSortable = false;
}
else
else if (DataType is not null)
{
var listItemType = dataGrid.ItemsSource.GetType().GetGenericArguments().Single();
var columnDataType = listItemType.GetProperty(PropertyName)?.PropertyType;

if (columnDataType is not null)
{
_isSortable = typeof(IComparable).IsAssignableFrom(columnDataType);
}
_isSortable = typeof(IComparable).IsAssignableFrom(DataType);
}
}
catch
Expand All @@ -357,6 +353,17 @@ public bool IsSortable(DataGrid dataGrid)
return _isSortable ??= false;
}

internal void InitializeDataType()
{
ArgumentNullException.ThrowIfNull(DataGrid);

if (DataType == null && EditCellTemplate == null)
{
var rowDataType = DataGrid.ItemsSource.GetType().GetGenericArguments().Single();
DataType = rowDataType.GetProperty(PropertyName)?.PropertyType;
}
}

private void OnSizeChanged() => _sizeChangedEventManager.HandleEvent(this, EventArgs.Empty, nameof(SizeChanged));

#endregion Methods
Expand Down

0 comments on commit e75a119

Please sign in to comment.