2011-03-26 36 views

cevap

39

Eğer sadece bir Range olarak RTB içeriğin sonunu kullanarak, bazı hızlı renklendirme yapmak ve belki en basit çözümdür biçimlendirme uygulamak istiyorsanız , Örneğin

TextRange rangeOfText1 = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd); 
    rangeOfText1.Text = "Text1 "; 
    rangeOfText1.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue); 
    rangeOfText1.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold); 

    TextRange rangeOfWord = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd); 
    rangeOfWord.Text = "word "; 
    rangeOfWord.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red); 
    rangeOfWord.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Regular); 

    TextRange rangeOfText2 = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd); 
    rangeOfText2.Text = "Text2 "; 
    rangeOfText2.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue); 
    rangeOfText2.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold); 

bu size metin biçimlendirme büyük bir esneklik sağlar gibi ben FlowDocument hakkında MSDN sayfasını okumanızı öneririz, daha gelişmiş bir çözüm arıyorsanız edin.

+0

Doküman özelliğini kullanın "rangeOfWord" ve "rangeOfText2" eksiksiz, onlardan nasıl concat? – xavendano

14

Bunu deneyebilirsiniz.

public TestWindow() 
{ 
    InitializeComponent(); 

    this.paragraph = new Paragraph(); 
    rich1.Document = new FlowDocument(paragraph); 

    var from = "user1"; 
    var text = "chat message goes here"; 
    paragraph.Inlines.Add(new Bold(new Run(from + ": ")) 
    { 
     Foreground = Brushes.Red 
    }); 
    paragraph.Inlines.Add(text); 
    paragraph.Inlines.Add(new LineBreak()); 
    this.DataContext = this; 
} 
private Paragraph paragraph; 

Yani RichTextBox nesnesinin "rangeOfText1" sonra

İlgili konular