MaterialDesignInXamlToolkit icon indicating copy to clipboard operation
MaterialDesignInXamlToolkit copied to clipboard

DataGrid gridlines too thick when background set to Transparent

Open yosefmah opened this issue 5 years ago • 4 comments

I think following some of the improvements in #1838 the DataGrid gridlines don't seem to play nicely when the DataGrid background is set to Transparent.

Example when the DataGrid background is set to Transparent:

image

And when the DataGrid background is set specifically to the brush:

image

yosefmah avatar Jun 29 '20 14:06 yosefmah

That is interesting. From measuring, it appears that the lines in both screenshots are in fact the same size, however the ones in the transparent case are certainly brighter. Will need to dig into this more to know what is going on.

Can you provide the exact RGB colors you were using for the lines and the backgrounds?

Keboo avatar Jun 30 '20 07:06 Keboo

Hi @Keboo , sure it is #3D4C5C. Although this also happens on the demo app in dark theme and its default background.

I believe the issue occurs on the RemoveAlphaBrushConverter. The Transparent background is passed to it as white.

I have worked around this for now by setting DataGrid background to look up the background of its parent control rather than be Transparent.

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
            => values[0] is SolidColorBrush brush
                ? new SolidColorBrush(RgbaToRgb(brush.Color, values[1]))
                : Binding.DoNothing;

        private static Color RgbaToRgb(Color rgba, object background)
        {
            var backgroundColor = background switch
            {
                Color c => c,
                SolidColorBrush b => b.Color,
                _ => Colors.White
            };

            var alpha = (double) rgba.A / byte.MaxValue;
            var alphaReverse = 1 - alpha;

            return Color.FromRgb(
                (byte) (alpha * rgba.R + alphaReverse * backgroundColor.R),
                (byte) (alpha * rgba.G + alphaReverse * backgroundColor.G),
                (byte) (alpha * rgba.B + alphaReverse * backgroundColor.B)
            );

yosefmah avatar Jun 30 '20 10:06 yosefmah

@yosefmah Thanks for the information, that is very helpful.

Keboo avatar Jul 03 '20 20:07 Keboo

I added the RemoveAlphaBrushConverter to work around a problem. Here is HorizontalGridLinesBrush without opacity.

image

here with opacity (set directly to MaterialDesignDivider)

image

You can notice, that the actual data cells have stronger lines. It seems that WPF renders the horizontal grid lines doubled.

That is the reason why the grid line is drawn without opacity. Which makes it necessary to get the background color. And Transparent breaks this obviously.

greuelpirat avatar Jul 11 '20 12:07 greuelpirat