<ContentPage.Resources>
<ResourceDictionary>
<DataTemplate x:Key="low" >
<Label Text="{Binding Freight}"
TextColor="White"
BackgroundColor="Red"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center" />
</DataTemplate>
<DataTemplate x:Key="average" >
<Label Text="{Binding Freight}"
TextColor="Black"
BackgroundColor="Yellow"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center" />
</DataTemplate>
<DataTemplate x:Key="high" >
<Label Text="{Binding Freight}"
TextColor="White"
BackgroundColor="Green"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center" />
</DataTemplate>
</ResourceDictionary>
</ContentPage.Resources>
//Resource file
<ContentPage.Resources>
<ResourceDictionary>
<local:StyleConverter x:Key="Textcolor"/>
<local:BackgroundColorConverter x:Key="Backgroundcolor"/>
</ResourceDictionary>
</ContentPage.Resources>
// GridTextColumn
<sfgrid:SfDataGrid.Columns>
<sfgrid:GridTextColumn HeaderText="Order ID" MappingName="OrderID">
<sfgrid:GridTextColumn.CellStyle>
<Style TargetType="sfgrid:GridCell">
<Setter Property="BackgroundColor" Value="{Binding OrderID,
Converter={StaticResource Backgroundcolor}}" />
<Setter Property="Foreground" Value="{Binding OrderID,
Converter={StaticResource Textcolor}}" />
</Style>
</sfgrid:GridTextColumn.CellStyle>
</sfgrid:GridTextColumn>
//GridTemplateColumn
<sfgrid:GridTemplateColumn MappingName="CustomerID">
<sfgrid:GridTemplateColumn.CellTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding OrderID}" TextColor="{Binding OrderID,
Converter={StaticResource Textcolor}}" BackgroundColor="{Binding OrderID,
Converter={StaticResource Backgroundcolor}}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
</StackLayout>
</DataTemplate>
</sfgrid:GridTemplateColumn.CellTemplate>
</sfgrid:GridTemplateColumn>
</sfgrid:SfDataGrid.Columns>
// IValueConverter Class
public class StyleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int _value = (int)value;
if (_value % 2 == 0)
return Color.Green;
return Color.Red;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
} |
Thanks. It didn't answer my question about the binding, but I did get this to work based on your sample. I was using autogenerating columns but converted to manual creation in XAML, and added the other bits, and it's working ok.
Strangely, Intellisense shows the binding (OrderID in the sample below) as unknown, but it still works.. maybe the other method would've worked after all. Anyway it's fine now.