C# SolidColorBrush to my Converter Class

1.8k Views Asked by At

i have an object of the type SolidColorBrush and it holds a SolidColorBrush.

Now i have a converter for my dataGrid which is binded to a list. Each row in this dataGrid will be colored by the Converter i have.

All is working fine, but how can i return my SolidColorBrush object instead of an static "Brushes.Red" for example.

My converter:

[ValueConversion(typeof(MainWindow.eErrorLevel), typeof(Brush))]
public class TypeToColourConverter : IValueConverter
{
    #region IValueConverter Members
    public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        MainWindow.eErrorLevel errorLevel = (MainWindow.eErrorLevel)value;



        switch (errorLevel)
        {
            case MainWindow.eErrorLevel.Information:
                return Brushes.Red;


            case MainWindow.eErrorLevel.Warning:
                return Brushes.Yellow;

            case MainWindow.eErrorLevel.Error:
                return Brushes.Red;

        }

        return Brushes.Gray;
    }

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

    #endregion
}

My converter is not in the MainWindow, if thats important And My SolidColorBrush object in my MainWindow which is public:

public CurrentColor CurrentColors = new CurrentColor();



    public class CurrentColor
    {
        public SolidColorBrush ERROR { get; set; }
        public SolidColorBrush WARNING { get; set; }
        public SolidColorBrush INFORMATION { get; set; }
    }

EDIT: my brushes can be dynamically set by the user itself

EDIT2: now its working thanks guys :)

2

There are 2 best solutions below

2
On BEST ANSWER

Like I said in my comments, here's an example, passing it as converterparameter, there are probably alternatives:

XAML

<Window x:Class="WpfApplicationTestColorConverter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplicationTestColorConverter"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:ErrorColors x:Key="Colors" />
        <local:TypeToColourConverter x:Key="ColorConverter" />
    </Window.Resources>
    <Grid>
        <ListBox x:Name="ListBox1" ItemsSource="{Binding MyObjects}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock 
                        Text="{Binding Title}" 
                        Background="{Binding ErrorLevel, 
                            Converter={StaticResource ColorConverter}, 
                            ConverterParameter={StaticResource Colors}}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

Code behide

public partial class MainWindow : Window
{
    public ObservableCollection<MyObject> MyObjects { get; } = new ObservableCollection<MyObject>();

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;

        // find the (static)resource
        var colors = (ErrorColors)FindResource("Colors");
        colors.ERROR = new SolidColorBrush(Colors.Red);
        colors.WARNING = new SolidColorBrush(Colors.Orange);
        colors.INFORMATION = new SolidColorBrush(Colors.Lime);

        // Add objects to the list
        MyObjects.Add(new MyObject { Title = "This is an error", ErrorLevel = ErrorLevel.Error });
        MyObjects.Add(new MyObject { Title = "This is a warning", ErrorLevel = ErrorLevel.Warning });
        MyObjects.Add(new MyObject { Title = "This is information", ErrorLevel = ErrorLevel.Information });
    }
}

The Converter

[ValueConversion(typeof(ErrorLevel), typeof(Brush))]
public class TypeToColourConverter : IValueConverter
{
    #region IValueConverter Members
    public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        if (!(value is ErrorLevel))
            return Brushes.Gray;

        if (!(parameter is ErrorColors))
            return Brushes.Gray;

        var lvl = (ErrorLevel)value;
        var currentColor = (ErrorColors)parameter;

        switch (lvl)
        {
            case ErrorLevel.Information:
                return currentColor.INFORMATION;


            case ErrorLevel.Warning:
                return currentColor.WARNING;

            case ErrorLevel.Error:
                return currentColor.ERROR;

        }

        return Brushes.Gray;
    }

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

    #endregion
}

public class ErrorColors
{
    public SolidColorBrush ERROR { get; set; }
    public SolidColorBrush WARNING { get; set; }
    public SolidColorBrush INFORMATION { get; set; }
}

public enum ErrorLevel
{
    Error,
    Warning,
    Information
}

public class MyObject
{
    public string Title { get; set; }
    public ErrorLevel ErrorLevel { get; set; }
}

Results:

ResultScreen

1
On

Assuming that these colours won't change at runtime, you could declare your brushes as resources above your converter and add properties to your converter for each brush as follows:

Amend your converter to:

[ValueConversion(typeof(MainWindow.eErrorLevel), typeof(Brush))]
public class TypeToColourConverter : IValueConverter
{
    #region IValueConverter Members
    public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        MainWindow.eErrorLevel errorLevel = (MainWindow.eErrorLevel)value;

        switch (errorLevel)
        {
            case MainWindow.eErrorLevel.Information:
                return Error;


            case MainWindow.eErrorLevel.Warning:
                return Warning;

            case MainWindow.eErrorLevel.Error:
                return Information;

        }

        return Normal;
    }

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

    #endregion

    public Brush Normal { get; set; }

    public Brush Error { get; set; }

    public Brush Warning { get; set; }

    public Brush Information { get; set; }
}

Amend your XAML (wherever your converter is added):

<SolidColorBrush x:Key="Normal" Color="#FFAAAAAA"/>
<SolidColorBrush x:Key="Error" Color="#FFFF0000"/>
<SolidColorBrush x:Key="Warning" Color="#FF00FF00"/>
<SolidColorBrush x:Key="Information" Color="#FF0000FF"/>

<local:TypeToColourConverter x:Key="TypeToColourConverter" Normal="{StaticResource Normal}" Error="{StaticResource Error}" Warning="{StaticResource Warning}" Information="{StaticResource Information}" />

This is very 'designer-friendly' (i.e. all these colours can then be changed in Blend) and easy to maintain.

Hope it helps.