Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a Color to a Brush in XAML?

I want to convert a System.Windows.Media.Color value to a System.Windows.Media.Brush. The color value is databound to a Rectangle object's Fill property. The Fill property takes a Brush object, so I need an IValueConverter object to perform the conversion.

Is there a built-in converter in WPF or do I need to create my own? How do I go about creating my own if it becomes necessary?

like image 467
dthrasher Avatar asked Jul 22 '10 14:07

dthrasher


People also ask

What is WPF brush?

The following list describes the different types of WPF brushes: SolidColorBrush: Paints an area with a solid Color. LinearGradientBrush: Paints an area with a linear gradient. RadialGradientBrush: Paints an area with a radial gradient. ImageBrush: Paints an area with an image (represented by an ImageSource object).


3 Answers

I know I am really late to the party, but you don't need a converter for this.

You could do

<Rectangle>
    <Rectangle.Fill>
        <SolidColorBrush Color="{Binding YourColorProperty}" />
    </Rectangle.Fill>
</Rectangle>
like image 182
Jens Avatar answered Oct 22 '22 02:10

Jens


It seems that you have to create your own converter. Here a simple example to start:

public class ColorToSolidColorBrushValueConverter : IValueConverter {

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        if (null == value) {
            return null;
        }
        // For a more sophisticated converter, check also the targetType and react accordingly..
        if (value is Color) {
            Color color = (Color)value;
            return new SolidColorBrush(color);
        }
        // You can support here more source types if you wish
        // For the example I throw an exception

        Type type = value.GetType();
        throw new InvalidOperationException("Unsupported type ["+type.Name+"]");            
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        // If necessary, here you can convert back. Check if which brush it is (if its one),
        // get its Color-value and return it.

        throw new NotImplementedException();
    }
}

To use it, declare it in the resource-section.

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

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

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

I have not tested it. Make a comment if its not working.

like image 28
HCL Avatar answered Oct 22 '22 02:10

HCL


A Converter is not needed here. You can define a Brush in XAML and use it. It would be better to define the Brush as a Resource so it can be used other places required.

XAML is as below:

<Window.Resources>
    <SolidColorBrush Color="{Binding ColorProperty}" x:Key="ColorBrush" />
</Window.Resources>
<Rectangle Width="200" Height="200" Fill="{StaticResource ColorBrush}" />
like image 35
Kylo Ren Avatar answered Oct 22 '22 01:10

Kylo Ren