【问题标题】:How to color text in a multiline based control如何在基于多行的控件中为文本着色
【发布时间】:2018-04-19 14:09:17
【问题描述】:

环境:WPF、C#

我想要什么?: 我想显示一个消息日志,其中不同类型的消息(信息、警告、错误)以不同的样式显示。

有什么问题?: 根据我给出的架构,我需要使用绑定来做到这一点。目前有一个带有 LogMessages 的集合,其中包含一个 MessageType(枚举)。这个集合是我可以用来创建我的绑定的。

在无数示例中,我看到了使用运行或直接访问富文本控件的解决方案。我做不到。

那么有没有办法使用绑定和 WPF 给定的控件来创建这种效果?也许使用转换器?最好以一种允许像使用常规文本框一样选择文本的方式。

感谢任何建议

谢谢, 维达

解决方案

尽管我希望实现一种整体文本框,用户可以在其中选择不同的消息,就像在浏览器中一样,我最终还是使用了样式触发器,就像下面建议的 Dairon。

我现在将这样做,以便用户可以选择多条消息并将它们作为字符串复制到缓存中。

    <ListBox ItemsSource="{Binding Messages}" BorderBrush="Black"

                HorizontalAlignment="Stretch" Margin="10,70,10,0" Height="107" VerticalAlignment="Top">
        <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Margin="4">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <TextBlock Text="{Binding Time}" FontWeight="DemiBold" />
                <TextBlock Grid.Column="1" Text="{Binding Message}">
                    <TextBlock.Style>
                        <Style TargetType="{x:Type TextBlock}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=MsgType}" Value="Info">
                                    <Setter Property="Foreground" Value="DarkGray" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Path=MsgType}" Value="Warning">
                                    <Setter Property="Foreground" Value="DarkOrange" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Path=MsgType}" Value="Error">
                                    <Setter Property="Foreground" Value="DarkRed" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>

            </Grid>
        </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

【问题讨论】:

  • 为什么不能使用不可编辑的文本框?
  • 您尝试过模板绑定吗?
  • @mm8 据我所知,TextBox 只允许直接访问的不同样式,而不是过度动态绑定?
  • @ChiragMM 我不能完全听从你的建议。在这种情况下,模板绑定如何允许我使用不同的样式?

标签: c# wpf binding multiline text-coloring


【解决方案1】:

我使用了 Federico Berasategui 的示例 here。这个例子很完美,你应该从这里开始。为了处理不同的颜色,我将他的代码更改为:

LogEntry 类:

public class LogEntry
{
    public LogEntry(DateTime dateTime, int index, string message, Color textColor)
    {
        DateTime = dateTime;
        Index = index;
        Message = message;
        TextColor = new SolidColorBrush(textColor);
        TextColor.Freeze();
    }

    public DateTime DateTime { get; set; }

    public int Index { get; set; }

    public string Message { get; set; }

    public SolidColorBrush TextColor { get; set; }
}

LogEntry 显示的 XAML 部分:

<DataTemplate DataType="{x:Type local:LogEntry}">
    <Grid IsSharedSizeScope="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition SharedSizeGroup="Index" Width="Auto"/>
            <ColumnDefinition SharedSizeGroup="Date" Width="Auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <TextBlock Text="{Binding DateTime}" Grid.Column="0"
                   FontWeight="Bold" Margin="5,0,5,0"/>

        <TextBlock Text="{Binding Index}" Grid.Column="1"
                   FontWeight="Bold" Margin="0,0,2,0" />

        <TextBlock Text="{Binding Message}" Grid.Column="2"
                   TextWrapping="Wrap" Foreground="{Binding TextColor}"/>
    </Grid>
</DataTemplate>

我刚刚在 LogEntry 类中添加了一个 TextColor 属性并将其绑定到 TextBlock 的 Foreground 属性。

如果您不需要多级日志,请随时从示例中删除 CollapsibleLogEntry 部分。当然,如果您只想处理日志中的文本消息,您可以删除 DateTime 和 Index。如果你想要一个 MessageType,你也可以添加你的 Enum。

这个例子相当完整,但如果你不想所有的“额外”,你可以做得很简单。

【讨论】:

  • 看起来棒极了。我会在接下来的几天内尝试实施它并给你反馈。
  • 你好戴隆。我按照您建议的方式实施了它,并且效果很好。不知道为什么我自己没有想到。非常感谢
猜你喜欢
  • 2013-07-07
  • 1970-01-01
  • 2021-04-18
  • 2017-07-07
  • 2021-11-19
  • 1970-01-01
  • 2018-08-15
  • 2018-05-23
  • 1970-01-01
相关资源
最近更新 更多