简体   繁体   English

如何在列表框 C# 中对行文本进行多色处理?

[英]How to multicolor row text in listbox C#?

i wanna do a log lister app in C#.我想在 C# 中做一个日志列表应用程序。 I have a listbox called LogBox and i wanna color multiple times a row.我有一个名为LogBox的列表框,我想连续多次着色。 like: "[04:30:20] - Admin: Hello" but each variable should be different color at row in Listbox.像:“[04:30:20] - 管理员:你好”,但每个变量在列表框中的行应该是不同的颜色。

How should i do this with button action?我应该如何使用按钮操作来做到这一点?

I tried LogBox.Items.Add(LogBox.ForeColor = color.red + "[" etc etc etc. but its doesn't work.我试过LogBox.Items.Add(LogBox.ForeColor = color.red + "[" etc etc etc. 但它不起作用。

I guess you might be looking for something like this.我猜你可能正在寻找这样的东西。

It can be easily achieved if you have a model class which is bound to your ListBox.如果您有一个绑定到您的 ListBox 的 model class ,则可以轻松实现。 Follow the below steps请按照以下步骤操作

Step 1 - Create a model class, say suppose "ListBoxItemModel.cs"第 1 步 -创建一个 model class,假设“ListBoxItemModel.cs”

public class ListBoxItemModel
{
    public string Text { get; set; }

    public Brush ForegroundBrush { get; set; }  
}

Note:- I am not following any MVVM approach here for demo.注意:-我在这里没有遵循任何 MVVM 方法进行演示。 If you are familiar then you can implement with this code.如果您熟悉,则可以使用此代码实现。

Step 2 - Create a window with ListBox and define a DataTemplate for your Model class as below in your MainWindow.第 2 步 -使用 ListBox 创建 window 并在 MainWindow 中为您的 Model class 定义一个 DataTemplate,如下所示。

Assign the DataTemplate to your ListBox ItemTemplate property.将 DataTemplate 分配给 ListBox ItemTemplate 属性。

<Window x:Class="SO61263305.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:SO61263305"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Window.Resources>
    <DataTemplate x:Key="LocalTemplate" DataType="local:ListBoxItemModel">
        <TextBlock Text="{Binding Text}" Foreground="{Binding ForegroundBrush}" />
    </DataTemplate>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <ListBox x:Name="ItemsListBox" Grid.Row="0" Height="200" Width="200" 
             ItemTemplate="{StaticResource LocalTemplate}"/>
</Grid>

Step 3 - Create an List of "ListBoxItemModel" and bound to ListBox from code-behind of your window or user control.第 3 步 -从 window 或用户控件的代码隐藏创建一个“ListBoxItemModel”列表并绑定到 ListBox。 In my case it is MainWindow.xaml.cs就我而言,它是 MainWindow.xaml.cs

private void LoadDataObjects()
{
        var items = new List<ListBoxItemModel>();
        var item = new ListBoxItemModel()
        {
            Text = "John ABCD 1",
            ForegroundBrush = new SolidColorBrush(Color.FromRgb(0, 0, 0))
        };

        items.Add(item);

        item = new ListBoxItemModel()
        {
            Text = "John ABCD 2",
            ForegroundBrush = new SolidColorBrush(Color.FromRgb(200, 79, 24))
        };

        items.Add(item);
        ItemsListBox.ItemsSource = items;
    }

In the above method you need to add each item with the Text which you wanted to display and Foreground Brush.在上述方法中,您需要为每个项目添加要显示的文本和前景画笔。

Step 4 - Call above method from your constructor of code-behind or else you can call from any other events like Button click to load the data to a listbox.第 4 步 -从代码隐藏的构造函数调用上述方法,否则您可以从任何其他事件(如按钮单击)调用以将数据加载到列表框。

See below of my complete MainWindow.xaml.cs (code behind of the MainWindow)请参阅下面我的完整 MainWindow.xaml.cs(MainWindow 后面的代码)

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        LoadDataObjects();
    }

    private void LoadDataObjects()
    {
        var items = new List<ListBoxItemModel>();
        var item = new ListBoxItemModel()
        {
            Text = "John ABCD 1",
            ForegroundBrush = new SolidColorBrush(Color.FromRgb(0, 0, 0))
        };

        items.Add(item);

        item = new ListBoxItemModel()
        {
            Text = "John ABCD 2",
            ForegroundBrush = new SolidColorBrush(Color.FromRgb(200, 79, 24))
        };

        items.Add(item);
        ItemsListBox.ItemsSource = items;
    }
}

Hope this should give you some idea and you can improve your requirements on top of it.希望这能给您一些想法,并且您可以在此基础上改进您的要求。

Give a try and let us know in case if you face any difficulties.试一试,如果您遇到任何困难,请告诉我们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM