Home > Software design >  WPF DataGrid change column backgroup disables selection background
WPF DataGrid change column backgroup disables selection background

Time:01-20

I have a data grid with value object in first name and last name the value object compares 2 string values and return enum of equal ,not equal

            <DataGridTextColumn Header="Pat. first name" 
                                Binding="{Binding Path=PatFirstName}">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell" >
                        <Setter Property="Background" Value="{Binding Path=PatFirstName.Result,Converter={StaticResource resultToColorConver}}"/>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>
        
            <DataGridTextColumn Header="Pat. last name" 
                                Binding="{Binding Path=PatLastName}">
            <DataGridTextColumn.CellStyle>
                <Style TargetType="DataGridCell" >
                        <Setter Property="Background" Value="{Binding Path=PatLastName.Result,Converter={StaticResource resultToColorConver}}"/>
                </Style>
            </DataGridTextColumn.CellStyle>
        </DataGridTextColumn>

and using this convertor

public class ResultToColorConvertor : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var result = (CompareResult)value;
        if(result == CompareResult.NotEqual)
        {
            return Brushes.Yellow;
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

but when selecting rows - I get this result :

enter image description here

how can I return to the selected blue background behavior (line in Patient ID or NDC columns) I tried Brushes.Transparent,DependencyProperty.UnsetValue and Binding.DoNothing;

CodePudding user response:

I suggest you to define the style on the Textbox of the Cell, instead of the CellStyle itself:

<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
    <Setter Property="Background" Value="{Binding Path=PatFirstName.Result,Converter={StaticResource resultToColorConver}}"/>
</Style>
</DataGridTextColumn.ElementStyle>

Using a transparent background should then work in your converter.

CodePudding user response:

you can change Background only if IsSelected=false:

<DataGridTextColumn.CellStyle>
    <Style TargetType="DataGridCell" >
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="False">
                 <Setter Property="Background" 
                         Value="{Binding Path=PatFirstName.Result,Converter={StaticResource resultToColorConver}}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGridTextColumn.CellStyle>
  •  Tags:  
  • Related