<ContentPage.Resources>
<ResourceDictionary>
//Converter declaration
<local:CustomGridCell x:Key="customGridCell" />
</ResourceDictionary>
</ContentPage.Resources>
//First column declaration and converter initialization
<sfgrid:GridTextColumn MappingName="OrderID">
<sfgrid:GridTextColumn.CellStyle>
<Style TargetType="sfgrid:GridCell">
<Setter Property="BackgroundColor" Value="{Binding .,Converter={StaticResource customGridCell}}" />
</Style>
</sfgrid:GridTextColumn.CellStyle>
</sfgrid:GridTextColumn> |
public class CustomGridCell : IValueConverter
{
public CustomGridCell()
{
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var rowData = value as OrderInfo;
if (rowData.OrderID < 100 && rowData.CustomerID > 150)
// Row color based on value
return Color.Olive;
else
//Fixed column background color for life
return Color.Navy;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
} |
<sfgrid:GridTextColumn MappingName="OrderID" LoadUIView="True">
<sfgrid:GridTextColumn.CellStyle>
<Style TargetType="sfgrid:GridCell">
<Setter Property="BackgroundColor" Value="{Binding .,Converter={StaticResource customGridCell}, ConverterParameter={x:Reference dataGrid}}" />
</Style>
</sfgrid:GridTextColumn.CellStyle>
</sfgrid:GridTextColumn> |
public Page1()
{
InitializeComponent();
dataGrid.CellRenderers.Remove("TextView");
dataGrid.CellRenderers.Add("TextView", new CustomGridCellTextViewRenderer());
} |
public class CustomGridCellTextViewRenderer : GridCellTextViewRenderer
{
public override void OnUpdateDisplayValue(DataColumnBase dataColumn, SfLabel view)
{
base.OnUpdateDisplayValue(dataColumn, view);
if (view!= null && view.Parent != null)
(view.Parent as CellElement).Style = null;
}
protected override void OnUpdateCellStyle(DataColumnBase dataColumn)
{
base.OnUpdateCellStyle(dataColumn);
dataColumn.UpdateColumn(dataColumn.RowData, true);
}
} |
public class CustomGridCell : IValueConverter
{
public CustomGridCell()
{
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var rowData = value as OrderInfo;
var dataGrid = parameter as SfDataGrid;
var selectedRow = dataGrid.SelectedItem;
if (rowData.OrderID < 100 && rowData.CustomerID > 150)
{
if ((selectedRow != null) && (selectedRow == rowData))
// Apply selection color when the row is selected
return Color.Transparent;
else
// Row color based on value
return Color.Olive;
}
else
{
if ((selectedRow != null) && (selectedRow == rowData))
// Apply selection color when the row is selected
return Color.Transparent;
else
//Fixed background color for life
return Color.Navy;
}
} |