Articles in this section
Category / Section

How to change FontWeight for particular column based on Cell Value in WPF DataGrid?

7 mins read

In WPF DataGrid, you can apply the style for every cell in a particular column based on CellValues. You can customize the FontWeight of the cells in a particular column, based on its cell content in two ways.

  1. Customize Style using Converters.
  2. CellStyleSelector.

Customize Style using Converters

You can customize the FontWeight of GridCell based on its data, by customizing its Style and Writing Converter for FontWeight property that converts the FontWeight based on bound data.  The customized style can be set to SfDataGrid.CellStyle or GridColumn.CellStyle property based on your requirement.

XAML

<!--Resources-->
<Window.Resources>
    <local:ChangeFontWeight x:Key="changefontweight"/>
    <Style TargetType="syncfusion:GridCell" x:Key="cellstyle">
      <Setter Property="FontWeight" Value="{Binding RelativeSource={RelativeSource Self},Converter = {StaticResource changefontweight}}" />
    </Style>
</Window.Resources>
<!— SfDataGrid Definition -->
<syncfusion:SfDataGrid x:Name="datagrid"
                            CellStyle="{StaticResource cellstyle}"
                            AllowEditing="True" 
                            LiveDataUpdateMode="AllowDataShaping"
                            AutoGenerateColumns="True"
                            ColumnSizer="Auto"
                            ItemsSource="{Binding Stocks}">

The following code example demonstrates the ChangeFontWeight Converter Class that returns the FontWeight of the text based on cell value.

C#

public class ChangeFontWeight : IValueConverter
{
    object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo info)
    {
        var cell = value as GridCell;
        if (cell.ColumnBase.GridColumn.MappingName == "Change")
        {
            var data = (cell.DataContext as StockData).Change;
            if (data >= 0)
                return FontWeights.Bold;
            else
                return FontWeights.Normal;
        } 
        return null;
    }
    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo info)
    {
        throw new NotImplementedException();
    }
}

 

Note:

You can apply the style for a particular column by using GridColumn.CellStyle in SfDataGrid.

 

CellStyleSelector

You can customize the FontWeight of GridCell based on its data, by customizing its style and writing the StyleSelector for FontWeight property that converts the FontWeight based on bound data.  The customized style can be set to SfDataGrid.CellStyleSelector or GridColumn.CellStyleSelector property based on your requirement.

XAML

<!--Resources-->
<Window.Resources>
    <local:StockCellStyleSelector x:Key="stockCellStyleSelector" />
</Window.Resources>
<!—Defining Resources in SfDataGrid -->
<syncfusion:SfDataGrid x:Name="datagrid"
                                    Grid.Row="1"
                                    Margin="10,0,30,30"
                                    LiveDataUpdateMode="AllowDataShaping"
                                    ColumnSizer="Auto" 
                                    CellStyleSelector="{StaticResource stockCellStyleSelector}" 
                                    NavigationMode="Row"
                                    ItemsSource="{Binding Stocks}" ></syncfusion:SfDataGrid.Columns>

The following code example demonstrates how to write the style for FontWeight property of GridCell in App.Xaml.

XAML

<Application.Resources>
         <Style x:Key="SymbolCellStyle" TargetType="syncfusion:GridCell">
            <Setter Property="FontWeight" Value="Bold" />
        </Style>        <Style x:Key="NormalCellStyle" TargetType="syncfusion:GridCell">
            <Setter Property="FontWeight" Value="Normal" />
        </Style>
    </Application.Resources>

The appropriate style is applied for a particular column depending on the row data in SelectStyle method of the StyleSelector Class.

The following code example describes the StockCellStyleSelector class that returns the style based on cell value.

C#

public class StockCellStyleSelector : StyleSelector
{
    public override Style SelectStyle(object item, DependencyObject container)
        {
            var gridcell = container as GridCell;
            if (gridcell.ColumnBase.GridColumn.MappingName == "Change")
            {
                var row = item as StockData;
                if (row != null)
                    if (row.Change >= 0)
                        return App.Current.Resources["SymbolCellStyle"] as Style;
                    else
                        return App.Current.Resources["NormalCellStyle"] as Style;
            }
        return base.SelectStyle(item, container);
    }
}

 

Note:

You can apply the style for a particular column by using GridColumn.CellStyleSelector in SfDataGrid.

 

The following screenshot shows changing FontWeight for a particular column based on CellValue.

 

CellValue

Figure 1: Changing FontWeight using CellValue

 

Sample Links:

WPF

WRT

SilverLight

UWP



Conclusion
I hope you enjoyed learning how to change FontWeight for particular column based on Cell Value in WPF DataGrid.
You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF DataGrid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied