繁体   English   中英

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

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

我想在 C# 中做一个日志列表应用程序。 我有一个名为LogBox的列表框,我想连续多次着色。 像:“[04:30:20] - 管理员:你好”,但每个变量在列表框中的行应该是不同的颜色。

我应该如何使用按钮操作来做到这一点?

我试过LogBox.Items.Add(LogBox.ForeColor = color.red + "[" etc etc etc. 但它不起作用。

我猜你可能正在寻找这样的东西。

如果您有一个绑定到您的 ListBox 的 model class ,则可以轻松实现。 请按照以下步骤操作

第 1 步 -创建一个 model class,假设“ListBoxItemModel.cs”

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

    public Brush ForegroundBrush { get; set; }  
}

注意:-我在这里没有遵循任何 MVVM 方法进行演示。 如果您熟悉,则可以使用此代码实现。

第 2 步 -使用 ListBox 创建 window 并在 MainWindow 中为您的 Model class 定义一个 DataTemplate,如下所示。

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

第 3 步 -从 window 或用户控件的代码隐藏创建一个“ListBoxItemModel”列表并绑定到 ListBox。 就我而言,它是 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;
    }

在上述方法中,您需要为每个项目添加要显示的文本和前景画笔。

第 4 步 -从代码隐藏的构造函数调用上述方法,否则您可以从任何其他事件(如按钮单击)调用以将数据加载到列表框。

请参阅下面我的完整 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;
    }
}

希望这能给您一些想法,并且您可以在此基础上改进您的要求。

试一试,如果您遇到任何困难,请告诉我们。

暂无
暂无

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

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