在RichtextBox WPF中格式化文本

时间:2010-11-23 14:58:04

标签: wpf text richtextbox formatting

我尝试在richTextBox中格式化文本,就像在Skype聊天中一样。

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

我想要所有1.最左边的列和最右边的列。

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

1 个答案:

答案 0 :(得分:1)

我的解决方案:

简单的解决方案是创建Table对象并添加到richtextbox的块,像这样的somothing:

        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);