改变WPFC#中部分文本的颜色和字体或RichTextBox的某些部分的颜色和字体。我正在使用C#WPF。例如richTextBox.AppendText("Text1"+word+"Text2");比如变量word是Text1,其他颜色和字体在Text2中。有可能,怎么办?如果您只想快速着色,使用RTB内容的末尾作为范围并应用格式可能是最简单的解决方案,例如TextRangerangeOfText1=newTextRange(richTextBox.Document.ContentEnd,richTextBox.Document.ContentEnd);rangeOfText1.Text="文本1";rangeOfText1.ApplyPropertyValue(TextElement.ForegroundProperty,Brushes.Blue);rangeOfText1.ApplyPropertyValue(TextElement.FontWeightProperty,FontWeights.Bold);TextRangerangeOfWord=newTextRange(richTextBox.Document.ContentEnd,richTextBox.Document)rangeOfWord.Text="单词";rangeOfWord.ApplyPropertyValue(TextElement.ForegroundProperty,Brushes.Red);rangeOfWord.ApplyPropertyValue(TextElement.FontWeightProperty,FontWeights.Regular);TextRangerangeOfText2=newTextRange(richTextBox.Document.ContentEnd,richTextBox.ContentEnd);rangeOfText2.Text="文本2";rangeOfText2.ApplyPropertyValue(TextElement.ForegroundProperty,Brushes.Blue);rangeOfText2.ApplyPropertyValue(TextElement.FontWeightProperty,FontWeights.Bold);如果您正在寻找更高级的解决方案,我建议您阅读FlowDocument上的MSDN页面,因为这为您设置文本格式提供了很大的灵活性。你可以试试看。publicTestWindow(){InitializeComponent();this.paragraph=newParagraph();rich1.Document=newFlowDocument(段落);varfrom="user1";vartext="这里是聊天信息";paragraph.Inlines.Add(newBold(newRun(from+":")){Foreground=Brushes.Red});段落。内联。添加文字);段落。内联。添加(新换行符());这。数据上下文=这个;}私人段落段落;所以使用RichTextBox的Document属性您需要使用RichTextBox的Document属性并向其添加一个Run。文档属性:http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.document.aspx运行:http://msdn.microsoft.com/en-us/library/system。windows.documents.run.aspx我自己创建了一个类来操作TextBlockText,TextBox...以上是C#学习教程:C#在WPF中改变部分文本的颜色和字体很有用,需要了解更多C#学习教程,希望大家多多关注—//////用于文本操作操作的类///publicclassTextManipulation{//////是在一个TextPointer内部操作一个特定的字符串Range(TextBlock,TextBox...)//////起点在哪里看///终点在哪里看///这是你要操作的字符串///新的FontStyle///新的FontWeight///新前景///新背景///新字体大小开始指针,结束指针、关键字、fontStyle、fontWeight、前景、背景、fontSize、null);}//////正在操作TextPointer范围内的特定字符串(TextBlock、TextBox...)//////起点在哪里看///终点在哪里看///这是字符串你想操作///新的FontStyle///新的FontWeight///新的前景///新的背景///新的FontSize///新的字符串(如果你想替换,可以是null)publicstaticvoidFromTextPointer(TextPointerstartPointer,TextPointerendPointer,stringkeyword,FontStylefontStyle,FontWeightfontWeight,Brushforeground,Brushbackground,doublefontSize,stringnewString){if(startPointer==null)thrownewArgumentNullException(nameof(startPointer))};如果(endPointer==null)抛出新的ArgumentNullException(nameof(endPointer));如果(字符串。IsNullOrEmpty(关键字))抛出新的ArgumentNullException(关键字);TextRange文本=newTextRange(startPointer,endPoin之三);TextPointercurrent=text.Start.GetInsertionPosition(LogicalDirection.Forward);while(current!=null){stringtextInRun=current.GetTextInRun(LogicalDirection.Forward);如果(!string.IsNullOrWhiteSpace(textInRun)){intindex=textInRun.IndexOf(keyword);if(index!=-1){TextPointerselectionStart=current.GetPositionAtOffset(index,LogicalDirection.Forward);TextPointerselectionEnd=selectionStart.GetPositionAtOffset(keyword.Length,LogicalDirection.Forward);TextRangeselection=newTextRange(selectionStart,selectionEnd);如果(!string.IsNullOrEmpty(newString))selection.Text=newString;selection.ApplyPropertyValue(TextElement.FontSizeProperty,fontSize);selection.ApplyPropertyValue(TextElement.FontStyleProperty,fontStyle);selection.ApplyPropertyValue(TextElement.FontWeightProperty,fontWeight);selection.ApplyPropertyValue(TextElement.ForegroundProperty,前景);selection.ApplyPropertyValue(TextElement.BackgroundProperty,背景);}}current=current.GetNextContextPosition(LogicalDirection.Forward);}}}用法,NewFontSize,"NewStringIfYouWant");转载请注明出处:
