簡體   English   中英

如何在基於多行的控件中為文本着色

[英]How to color text in a multiline based control

環境 :WPF,C#

我想要什么? :我想顯示一條消息日志,其中以不同的樣式顯示不同類型的消息(信息,警告,錯誤)。

在此處輸入圖片說明

有什么問題? :基於我給出的體系結構,我需要使用綁定來實現。 當前,有一個LogMessages集合,其中包含一個MessageType(枚舉)。 我可以使用此集合來創建綁定。

在無數示例中,我看到了使用“運行”或直接訪問Rich Text控件的解決方案。 我做不到

那么有沒有辦法使用綁定和WPF給定的控件來創建這種效果? 也許使用Converter? 最好采用一種允許您像選擇常規文本框那樣僅選擇文本的方式。

感謝任何建議

謝謝,維達

即使我希望實現一種總體文本框,用戶可以在其中選擇不同的消息,就像在瀏覽器中一樣,但最終還是使用了樣式觸發器,就像下面建議的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>

我在這里使用了Federico Berasategui的示例。 這個例子很完美,您應該從這里開始。 為了處理不同的顏色,我將他的代碼更改為:

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屬性,並將其綁定到TextBlockForeground屬性。

如果不需要多級日志,請從示例中刪除CollapsibleLogEntry部分。 當然,如果您只想處理日志中的短信,則可以刪除DateTime和Index。 如果需要MessageType,也可以添加Enum。

該示例相當完整,但是如果您不想要所有的“附加”,則可以非常簡單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM