如何在列表框C#中使用多色行文本?
我想用C#做一个日志列表程序。我有一个名为如何在列表框C#中使用多色行文本?,c#,colors,listbox,C#,Colors,Listbox,我想用C#做一个日志列表程序。我有一个名为LogBox的列表框,我想为一行添加多次颜色。比如:“[04:30:20]-Admin:Hello”,但每个变量在列表框中的行中的颜色应该不同 我应该如何使用按钮操作来执行此操作 我尝试了LogBox.Items.Add(LogBox.ForeColor=color.red+“[”等,但它不起作用。我想你可能正在寻找类似的东西 如果您有一个绑定到ListBox的模型类,就可以很容易地实现它。 按照以下步骤操作 步骤1-创建一个模型类,假设“ListBox
LogBox
的列表框,我想为一行添加多次颜色。比如:“[04:30:20]-Admin:Hello”,但每个变量在列表框中的行中的颜色应该不同
我应该如何使用按钮操作来执行此操作
我尝试了LogBox.Items.Add(LogBox.ForeColor=color.red+“[”等,但它不起作用。我想你可能正在寻找类似的东西 如果您有一个绑定到ListBox的模型类,就可以很容易地实现它。 按照以下步骤操作 步骤1-创建一个模型类,假设“ListBoxItemModel.cs” 注意:-我在这里不使用任何MVVM方法进行演示。如果您熟悉,那么可以使用此代码实现 步骤2-使用ListBox创建一个窗口,并在主窗口中为模型类定义一个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-创建一个“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;
}
private void LoadDataObjects()
{
var items=新列表();
var item=new ListBoxItemModel()
{
Text=“John ABCD 1”,
ForegroundBrush=新的SolidColorBrush(Color.FromRgb(0,0,0))
};
项目。添加(项目);
item=新ListBoxItemModel()
{
Text=“John ABCD 2”,
ForegroundBrush=新的SolidColorBrush(颜色来自RGB(200,79,24))
};
项目。添加(项目);
ItemsListBox.ItemsSource=项目;
}
在上面的方法中,您需要使用要显示的文本和前景笔刷添加每个项目
步骤4-从代码隐藏的构造函数调用上述方法,或者您可以从任何其他事件(如单击按钮)调用,以将数据加载到列表框中
请参见下面完整的MainWindow.xaml.cs(主窗口的代码隐藏)
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
LoadDataObjects();
}
私有void LoadDataObjects()
{
var items=新列表();
var item=new ListBoxItemModel()
{
Text=“John ABCD 1”,
ForegroundBrush=新的SolidColorBrush(Color.FromRgb(0,0,0))
};
项目。添加(项目);
item=新ListBoxItemModel()
{
Text=“John ABCD 2”,
ForegroundBrush=新的SolidColorBrush(颜色来自RGB(200,79,24))
};
项目。添加(项目);
ItemsListBox.ItemsSource=项目;
}
}
希望这会给你一些想法,你可以在上面改进你的需求
如果您遇到任何困难,请尝试一下并告诉我们。类似吗?您使用什么GUI框架?WinForms或WPF?始终指定一个标记。
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;
}
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;
}
}