C#SolidColorBrush到我的Converter类

时间:2016-09-14 10:03:15

标签: c# .net wpf solidcolorbrush

我有一个SolidColorBrush类型的对象并且它成立 一个SolidColorBrush。

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

一切正常,但我怎么能返回我的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中,如果这很重要的话 我的MainWindow中的SolidColorBrush对象是公共的:

public CurrentColor CurrentColors = new CurrentColor();



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

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

EDIT2:现在正在努力工作:)

2 个答案:

答案 0 :(得分:1)

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

将您的转换器修改为:

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

这是非常“设计师友好的”#39; (即所有这些颜色可以在Blend中更改)并且易于维护。

希望它有所帮助。

答案 1 :(得分:0)

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

<强> XAML

<div id="Rss">

<div class="Rss-Title"></div>

<div class="Rss-Author"></div>

<div class="Rss-Description"></div>

</div>

代码隐藏

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

结果:

ResultScreen