Home > Software design >  how could I set the cell style in a column in a datagrid dynamically with converter or datatrigger?
how could I set the cell style in a column in a datagrid dynamically with converter or datatrigger?

Time:04-21

I would like to set dynamically the style of a row in a column of a datagrid according to a data in the data row, so according to the value of that property, only one property, I would like to choose between 3 different styles that I have in a resource dictionary.

I am trying this solution:

<DataGridTextColumn.CellStyle>
    <Style TargetType="DataGridCell" BasedOn="{StaticResource ResourceKey=DataGridCellLeftHorizontalAlignment}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding NumeroLineaFactura}" Value="-1">
                <Setter Property="FontWeight" Value="Bold"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGridTextColumn.CellStyle>

But this has the problem that I can only select the one of the styles and I have to change the rest of the properties one by one, so how I have to set the style in various columns, it makes me to repeat the code.

I would like to can select the style with a data trigger or with a converter, but I don't know the syntax to can do it, because in the trigger I can set the property Style neither CellStryle.

How could I set the cell style dynamically?

Thanks.

CodePudding user response:

Since there is no CellStyleSelector property available and you cannot use a DataTrigger to change the Style itself, you should use a single Style and then use several DataTriggers to change the properties of the DataGridCell, e.g.:

 <Style TargetType="DataGridCell">
    <Style.Triggers>
        <DataTrigger Binding="{Binding NumeroLineaFactura}" Value="-1">
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="TextAlignment" Value="Right"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

Thanks. In this case it works, but if I have another column that for the -1 value has the same style, I have to repeat the code...

Right. In this case you could consider implementing your own custom DataGridTextColumn and programmatically set the CellStyle. Something like this:

public class CustomDataGridTextColumn : DataGridTextColumn
{
    public static readonly DependencyProperty FirstStyleProperty = DependencyProperty.Register(
        name: nameof(FirstStyle),
        propertyType: typeof(Style),
        ownerType: typeof(CustomDataGridTextColumn)));

    public Style FirstStyle
    {
        get => (Style)GetValue(FirstStyleProperty);
        set => SetValue(FirstStyleProperty, value);
    }

    //...

    protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
    {
        FrameworkElement element = base.GenerateElement(cell, dataItem);

        var val = dataItem as YourClass;
        if(val != null)
        {
            switch (val.NumeroLineaFactura)
            {
                case -1:
                    cell.Style = FirstStyle;
                    break;
                //...
            }
        }

        return element;
    }
}

XAML::

<DataGrid.Columns>
    <local:CustomDataGridTextColumn ... FirstStyle="{StaticResource yourFirstStyle}" />
</DataGrid.Columns>
  •  Tags:  
  • wpf
  • Related