C# SolidColorBrush 到我的转换器 Class

C# SolidColorBrush to my Converter Class

我有一个 SolidColorBrush 类型的对象,它包含 SolidColorBrush.

现在我有一个绑定到列表的 dataGrid 转换器。 此数据网格中的每一行都将由我拥有的转换器着色。

一切正常,但我如何 return 我的 SolidColorBrush 对象而不是静态 "Brushes.Red" 例如。

我的转换器:

[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
}

我的转换器不在主窗口中,如果那很重要的话 我的 MainWindow 中的 SolidColorBrush 对象是 public:

public CurrentColor CurrentColors = new CurrentColor();



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

编辑:我的画笔可以由用户自己动态设置

EDIT2:现在可以正常工作了,谢谢大家:)

假设这些颜色在运行时不会改变,您可以将画笔声明为转换器上方的资源,并为每个画笔向转换器添加属性,如下所示:

将您的转换器修改为:

[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; }
}

修改你的XAML(无论你的转换器添加到哪里):

<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}" />

这非常 'designer-friendly'(即所有这些颜色都可以在 Blend 中更改)并且易于维护。

希望对您有所帮助。

正如我在评论中所说,这是一个示例,将其作为转换器参数传递,可能还有其他选择:

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>

隐藏代码

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 });
    }
}

转换器

[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; }
}

结果: