Álvaro García
Álvaro García

Reputation: 19356

how to convert a SystemColors to Brush?

I have a converter that return a Brush to set the background of a control in my view. However, when I return the SystemColors.XXX it does not work, however when I use a Brush then it works, so I am thinking that I need to convert the SystemColors to a Brush.

How can I do it? Because I have tried this:

return (Brush)System.Windows.SystemColors.HighlightTextBrush;

In the resources of the control I set this:

<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Transparent"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="Black"/>

I am using the transparent because the the background of the row I will set them by a multi value converter.

thank so much.

Upvotes: 0

Views: 1564

Answers (1)

Ajden Towfeek
Ajden Towfeek

Reputation: 387

You need to create your own converter

public class ColorToSolidColorBrushValueConverter : IValueConverter {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            return new SolidColorBrush((Color)value);
        }

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

Declare it in the resource-section to use it.

<local:ColorToSolidColorBrushValueConverter  x:Key="ColorToSolidColorBrushValueConverter"/>

And the use it in the binding as a static resource.

Fill="{Binding Path=xyz,Converter={StaticResource ColorToSolidColorBrush_ValueConverter}}"

Upvotes: 1

Related Questions