简体   繁体   中英

DataGrid Change Background of a single cell

I am trying to set the background color of a single cell in my ´DataGrid´, but everytime I set the color to a DataGridCell, the background of the entire row is overwritten. My code:

 <Setter Property="CellStyle">
                    <Setter.Value>
                        <Style TargetType="{x:Type DataGridCell}">
                            <Setter Property="Background" Value="{Binding id, Converter={StaticResource TheConverter}}" />
                        </Style>
                    </Setter.Value>
                </Setter>


public class CellHighlighterConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value.ToString() == "1") return new SolidColorBrush(Colors.Red);

        return null;
    }

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

So the problem is... the entire row with the ID "1" is red! Not just the id cell. What is the right way to do this?

You can try the following workaround:

  1. Wrap the cell content with a Grid.
  2. Set the Grid to stretch both horizontally and vertically.
  3. Now change the background color of the grid, instead the background color of the cell.

I haven't tested it myself, but I think its worth a shot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM