我有一个ListView
,我使用TextWriter
的实现将输出重定向到如下:
ScriptEngine pyEngine;
pyEngine.Runtime.IO.RedirectToConsole();
Console.SetOut(TextWriter.Synchronized(new ListViewWriter(lbIpyOutput, Dispatcher)));
我希望根据枚举值实现ListView item
的彩色背景。
在这里的大多数示例中,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++;
}
ListViewWriter
是TextWriter
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
EDIT1 枚举仅用于内部日志记录,如下所示:
enum Severity{CRITIC, WARNING, DEBUG, MESSAGE, ERROR};
答案 0 :(得分:0)
我认为这对您有用,但如果您有任何疑问请与我联系。
忘记覆盖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;
}
}
}
结果:
再次,让我知道这是不是可以做到这一点&amp;我会尝试解决它。谢谢!