sách gpt4 ai đã đi

c# - 根据 TextWriter 输入 wpf 为 ListView 分配背景颜色

In lại 作者:行者123 更新时间:2023-11-30 18:19:18 26 4
mua khóa gpt4 Nike

Tôi có một cái 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 Đúng 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
phá vỡ;
case Severity.DEBUG:
bgColor = "3498db"; //peter river blue
phá vỡ;
case Severity.ERROR:
bgColor = "e74c3c"; //alizarin red
phá vỡ;
case Severity.MESSAGE:
bgColor = "95a5a6"; //concrete grey
phá vỡ;
case Severity.WARNING:
bgColor = "9b59b6"; //amethyst purple
phá vỡ;
}

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

1 Câu trả lời

我认为这应该对你有用,但如果你有任何问题,请告诉我。

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

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

Mới 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”继承。如果需要/您可以将它们添加回去。

Mới转换函数:

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

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

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

return brush;
}

}

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

主窗口.xaml:


xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">





主窗口.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
phá vỡ;
case Severity.DEBUG:
bgColor = "3498db"; //peter river blue
phá vỡ;
case Severity.ERROR:
bgColor = "e74c3c"; //alizarin red
phá vỡ;
case Severity.MESSAGE:
bgColor = "95a5a6"; //concrete grey
phá vỡ;
case Severity.WARNING:
bgColor = "9b59b6"; //amethyst purple
phá vỡ;
}

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

return brush;
}

}
}

kết quả:

Results

再一次,如果这不能解决问题,请告诉我,我会尝试修复它。谢谢!

关于c# - 根据 TextWriter 输入 wpf 为 ListView 分配背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39167284/

26 4 0
Bài viết được đề xuất: c# - 从空间中的工作点获取节点
Bài viết được đề xuất: c# - `gulp.watch` 阻塞执行 `dotnet run`
Bài viết được đề xuất: c# - 为什么Selenium GECKO driver for Firefox 48 无法启动服务器?
Bài viết được đề xuất: JavaScript 旋转
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com