【问题标题】:Formatting text in RichtextBox WPF在 RichtextBox WPF 中格式化文本
【发布时间】:2011-05-14 12:39:20
【问题描述】:

我尝试在richTextBox 中格式化文本,类似于Skype 聊天。

1.column-"Nick"         2.column-"Text of Messange"                3.column-"DateTime" 

我想要 1. 列最大左侧和 3. 列最大右侧。

最好的方法是什么?我正在使用 WPF。

【问题讨论】:

  • 你为什么使用一个 RichTextBox(而不是三个 (Rich)TextBlocks)?
  • 我认为这不是一个好的解决方案,例如如果你有完整的richtextbox,你必须滚动3个richtetbox,你也有richtextboxes中的行高问题......

标签: wpf text richtextbox formatting


【解决方案1】:

我的解决方案:

简单的解决方案是创建 Table 对象并添加到 Richtextbox 块中,如下所示:

        var tab = new Table();

        var gridLenghtConvertor = new GridLengthConverter();

        tab.Columns.Add(new TableColumn() { Name = "colNick", Width = (GridLength)gridLenghtConvertor.ConvertFromString("*") });
        tab.Columns.Add(new TableColumn { Name = "colMsg", Width = (GridLength)gridLenghtConvertor.ConvertFromString("5*") });
        tab.Columns.Add(new TableColumn() { Name = "colDt", Width = (GridLength)gridLenghtConvertor.ConvertFromString("*") });

        tab.RowGroups.Add(new TableRowGroup());
        tab.RowGroups[0].Rows.Add(new TableRow());

        var tabRow = tab.RowGroups[0].Rows[0];


        tabRow.Cells.Add(new TableCell(new Paragraph(new Run(rpMsg.Nick))) { TextAlignment = TextAlignment.Left });

        tabRow.Cells.Add(new TableCell(ConvertToRpWithEmoticons(rpMsg.RpText)));

        tabRow.Cells.Add(new TableCell(new Paragraph(new Run("Cas"))) { TextAlignment = TextAlignment.Right });

        RtbConversation.Document.Blocks.Add(tab);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多