Skip to content

Commit

Permalink
Merge pull request #144 from symbiogenesis/improve-editcelltemplate
Browse files Browse the repository at this point in the history
auto detect data type for default edit template
  • Loading branch information
symbiogenesis authored Dec 29, 2023
2 parents b4a35a6 + e3aa470 commit d279e70
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 26 deletions.
1 change: 1 addition & 0 deletions Maui.DataGrid/DataGridColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace Maui.DataGrid;
using Maui.DataGrid.Extensions;
using Microsoft.Maui.Controls.Shapes;
using System.ComponentModel;
using System.Reflection;

/// <summary>
/// Specifies each column of the DataGrid.
Expand Down
179 changes: 153 additions & 26 deletions Maui.DataGrid/DataGridRow.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace Maui.DataGrid;

using System.Globalization;
using Maui.DataGrid.Extensions;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Layouts;

internal sealed class DataGridRow : Grid
{
Expand Down Expand Up @@ -154,42 +156,167 @@ private View CreateViewCell(DataGridColumn col)

private View CreateEditCell(DataGridColumn col)
{
View cell;
var cell = GenerateTemplatedEditCell(col);

if (col.EditCellTemplate != null)
if (cell != null)
{
cell = new ContentView
{
BackgroundColor = _bgColor,
Content = col.EditCellTemplate.CreateContent() as View
};
return cell;
}

if (!string.IsNullOrWhiteSpace(col.PropertyName))
{
cell.SetBinding(BindingContextProperty,
new Binding(col.PropertyName, source: BindingContext));
}
switch (Type.GetTypeCode(col.DataType))
{
case TypeCode.String:
return GenerateTextEditCell(col);
case TypeCode.Boolean:
return GenerateBooleanEditCell(col);
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.SByte:
case TypeCode.Single:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
return GenerateNumericEditCell(col);
case TypeCode.DateTime:
return GenerateDateTimeEditCell(col);;
}
else

return new TemplatedView { BackgroundColor = _bgColor };
}

private ContentView? GenerateTemplatedEditCell(DataGridColumn col)
{
if (col.EditCellTemplate == null)
{
cell = new Entry
{
TextColor = _textColor,
BackgroundColor = _bgColor,
VerticalTextAlignment = col.VerticalTextAlignment,
HorizontalTextAlignment = col.HorizontalTextAlignment,
FontSize = DataGrid.FontSize,
FontFamily = DataGrid.FontFamily
};
return null;
}

if (!string.IsNullOrWhiteSpace(col.PropertyName))
var cell = new ContentView
{
BackgroundColor = _bgColor,
Content = col.EditCellTemplate.CreateContent() as View
};

if (!string.IsNullOrWhiteSpace(col.PropertyName))
{
cell.SetBinding(BindingContextProperty,
new Binding(col.PropertyName, source: BindingContext));
}

return cell;
}

private Grid GenerateTextEditCell(DataGridColumn col)
{
var entry = new Entry
{
TextColor = _textColor,
BackgroundColor = _bgColor,
VerticalTextAlignment = col.VerticalTextAlignment,
HorizontalTextAlignment = col.HorizontalTextAlignment,
FontSize = DataGrid.FontSize,
FontFamily = DataGrid.FontFamily
};

if (!string.IsNullOrWhiteSpace(col.PropertyName))
{
entry.SetBinding(Entry.TextProperty,
new Binding(col.PropertyName, BindingMode.TwoWay, stringFormat: col.StringFormat, source: BindingContext));
}

return WrapViewInGrid(entry);
}

private Grid GenerateBooleanEditCell(DataGridColumn col)
{
var checkBox = new CheckBox
{
Color = _textColor,
BackgroundColor = _bgColor,
};

if (!string.IsNullOrWhiteSpace(col.PropertyName))
{
checkBox.SetBinding(CheckBox.IsCheckedProperty,
new Binding(col.PropertyName, BindingMode.TwoWay, source: BindingContext));
}

return WrapViewInGrid(checkBox);
}

private Grid GenerateNumericEditCell(DataGridColumn col)
{
var stackLayout = new FlexLayout
{
Wrap = FlexWrap.Wrap,
Direction = FlexDirection.Row,
AlignContent = FlexAlignContent.Center,
AlignItems = FlexAlignItems.Center,
JustifyContent = FlexJustify.Center,
};

var label = new Label
{
TextColor = _textColor,
VerticalTextAlignment = TextAlignment.Center,
};

var stepper = new Stepper
{
BackgroundColor = DeviceInfo.Platform == DevicePlatform.WinUI ? _textColor : null,
};

stepper.ValueChanged += (b, e) =>
{
if (b is Stepper s)
{
cell.SetBinding(Entry.TextProperty,
new Binding(col.PropertyName, BindingMode.TwoWay, stringFormat: col.StringFormat, source: BindingContext));
label.Text = s.Value.ToString(CultureInfo.InvariantCulture);
}
};

stackLayout.Add(label);
stackLayout.Add(stepper);

if (!string.IsNullOrWhiteSpace(col.PropertyName))
{
label.SetBinding(Label.TextProperty,
new Binding(col.PropertyName, BindingMode.TwoWay, source: BindingContext));
stepper.SetBinding(Stepper.ValueProperty,
new Binding(col.PropertyName, BindingMode.TwoWay, source: BindingContext));
}

return cell;
return WrapViewInGrid(stackLayout);
}

private Grid GenerateDateTimeEditCell(DataGridColumn col)
{
var datePicker = new DatePicker
{
TextColor = _textColor,
};

if (!string.IsNullOrWhiteSpace(col.PropertyName))
{
datePicker.SetBinding(DatePicker.DateProperty,
new Binding(col.PropertyName, BindingMode.TwoWay, source: BindingContext));
}

return WrapViewInGrid(datePicker);
}

private Grid WrapViewInGrid(View view)
{
var grid = new Grid
{
BackgroundColor = _bgColor,
};

grid.Add(view);

return grid;
}

private void UpdateColors()
Expand Down

0 comments on commit d279e70

Please sign in to comment.