【问题标题】:Assign Background Color to ListView based on TextWriter input wpf根据TextWriter输入wpf为ListView分配背景颜色
【发布时间】:2016-08-26 13:16:55
【问题描述】:

我有一个ListView,我使用TextWriter 的实现将输出重定向到如下:

ScriptEngine pyEngine;
pyEngine.Runtime.IO.RedirectToConsole();
Console.SetOut(TextWriter.Synchronized(new ListViewWriter(lbIpyOutput, Dispatcher)));

我想根据 Enum 值实现ListView item 的彩色背景。

在 SO 上的大多数示例中,DataBinding 用于作为类的属性。对我来说,颜色不依赖于任何属性,而是Enum。 Atm 我使用一种方法来格式化输出。

public static void WriteToConsole(string _stringToWrite)  
{
    string output = String.Format("PY | {0} | {1} | {2}", tagCount, DateTime.Now, _stringToWrite);   
    Console.WriteLine(output);
    tagCount++;
}

ListViewWriterTextWriter 的实现

class ListViewWriter : TextWriter
    {
        private ListView listView;

        public ListViewWriter(ListView _listBox, Dispatcher _dispatcher)
        {
            listView = _listBox;
        }

        public override void WriteLine(string value)
        {
            base.WriteLine(value);
            listView.Items.Add(value.ToString());
        }
    }

由于DataBinding 似乎不是我想要覆盖Console.WriteLine(String _stringToWrite, MyEnum enum) 的选项,但这显然不起作用,因为TextWriter 发现“没有合适的方法来覆盖”。

我也创建了一个转换器类,但不知道如何充分利用(不确定是否真的可以使用十六进制值作为字符串):

class OutputBackgroundConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Severity severity = (Severity)value;
            string bgColor = "Gray";

            switch (severity)
            {
                case Severity.CRITIC:
                    bgColor = "27ae60"; //carrot orange
                    break;
                case Severity.DEBUG:
                    bgColor = "3498db"; //peter river blue
                    break;
                case Severity.ERROR:
                    bgColor = "e74c3c"; //alizarin red
                    break;
                case Severity.MESSAGE:
                    bgColor = "95a5a6"; //concrete grey
                    break;
                case Severity.WARNING:
                    bgColor = "9b59b6"; //amethyst purple
                    break;
            }

            return bgColor;
        }

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

如果有人可以在这里帮助我,那就太好了。我真的不知道如何解决这个问题,而且我对 WPF 还很陌生。

网络版本:4.5.1

编辑1 枚举仅用于内部日志记录,如下所示:

enum Severity{CRITIC, WARNING, DEBUG, MESSAGE, ERROR};

【问题讨论】:

  • 你从哪里得到严重性?这将帮助我回答这个问题。例如,如果您有一个函数 WriteLine(string str, Severity sev),则必须传入 Severity,但该 Severity 是从哪里来的?你能用那个代码更新你的问题吗?
  • 抱歉回复晚了。请参阅枚举的编辑。

标签: c# .net wpf


【解决方案1】:

我认为这应该适合您,但如果您有任何问题,请告诉我。

忘记覆盖WriteLine 函数。只需将其封装在您自己的可以处理枚举参数的函数中,然后像以前一样调用基本功能。

我添加的部分每次添加新项目时都会更改ListView中最后一个项目的背景颜色。

新增WriteLineWithEnum功能:

public void WriteLineWithEnum(string value, Severity severity)
{
    ListViewItem item = new ListViewItem();
        item.Content = value.ToString();
        SolidColorBrush bgColorBrush = OutputBackgroundConverter.Convert(severity);
        item.Background = bgColorBrush;
        myListView.Items.Add(item);
}

然后增强您的Convert 函数以返回一个SolidColorBrush 对象,该对象在上面用于更改ListViewItem 的背景颜色。只是提醒一下,我将其设为静态并删除了“IValueConverter”继承。如果您愿意/需要,您可以将它们添加回来。

新的Convert函数:

class OutputBackgroundConverter
{
    public static SolidColorBrush Convert(Severity severity)
    {
        string bgColor = "d3d3d3"; //Gray;

        switch (severity)
        {
            case Severity.CRITIC:
                bgColor = "27ae60"; //carrot orange
                break;
            case Severity.DEBUG:
                bgColor = "3498db"; //peter river blue
                break;
            case Severity.ERROR:
                bgColor = "e74c3c"; //alizarin red
                break;
            case Severity.MESSAGE:
                bgColor = "95a5a6"; //concrete grey
                break;
            case Severity.WARNING:
                bgColor = "9b59b6"; //amethyst purple
                break;
        }

        SolidColorBrush brush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#" + bgColor));

        return brush;
    }

}

我制作了一个快速演示应用程序来封装这个想法。代码和结果如下。

MainWindow.xaml:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView Name="myListView" Margin="20">
        </ListView>
    </Grid>
</Window>

MainWindow.xaml.cs:

using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfApplication2
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            System.Random rand = new System.Random();

            for (int i = 0; i < 10; i++)
            {
                int randInt = rand.Next(100, 1000);
                string randStr = randInt.ToString();
                this.AddListViewItem(randStr, (Severity)(i % 5)); //5 is number of Severities
            }
        }

        public void AddListViewItem(string value, Severity severity)
        {
            ListViewItem item = new ListViewItem();
            item.Content = value.ToString();
            SolidColorBrush bgColorBrush = OutputBackgroundConverter.Convert(severity);
            item.Background = bgColorBrush;
            myListView.Items.Add(item);
        }
    }

    public enum Severity
    {
        CRITIC = 0,
        DEBUG = 1,
        ERROR = 2,
        MESSAGE = 3,
        WARNING = 4,
    }

    class OutputBackgroundConverter
    {
        public static SolidColorBrush Convert(Severity severity)
        {
            string bgColor = "d3d3d3"; //Gray;

            switch (severity)
            {
                case Severity.CRITIC:
                    bgColor = "27ae60"; //carrot orange
                    break;
                case Severity.DEBUG:
                    bgColor = "3498db"; //peter river blue
                    break;
                case Severity.ERROR:
                    bgColor = "e74c3c"; //alizarin red
                    break;
                case Severity.MESSAGE:
                    bgColor = "95a5a6"; //concrete grey
                    break;
                case Severity.WARNING:
                    bgColor = "9b59b6"; //amethyst purple
                    break;
            }

            SolidColorBrush brush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#" + bgColor));

            return brush;
        }

    }
}

结果:

再一次,如果这不起作用,请告诉我,我会尝试修复它。谢谢!

【讨论】:

  • 嗨@james,感谢您的回答和努力。您在回答中没有考虑的一件事是控制台输出被重定向到ListView,因此覆盖TextWriter 的类ListViewWriter 负责将Console.WriteLine() 输出到我的ListView跨度>
  • 感谢您的反馈。我会再考虑一下。你需要从 TextWriter 继承吗?
猜你喜欢
  • 2021-03-28
  • 2021-03-07
  • 2021-08-29
  • 1970-01-01
  • 1970-01-01
  • 2017-08-12
  • 1970-01-01
  • 1970-01-01
  • 2022-08-13
相关资源
最近更新 更多