winform中的richtextbox比较熟悉
选择文本 richtextboxselection start richtextboxselection end
然后就是设置字体,richtextbox.selectionfont selectionbackcolor之类的
那么在wpf当中怎么设置richtextbox的字体呢
TextRange textRange = new TextRange(
// TextPointer to the start of content in the RichTextBox.
richtext_claim.Document.ContentStart,
// TextPointer to the end of content in the RichTextBox.
richtext_claim.Document.ContentEnd
);
//textRange.ApplyPropertyValue(TextElement.BackgroundProperty, "yellow");
textRange.ApplyPropertyValue(TextElement.BackgroundProperty, "yellow");
textRange.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline);
其中具体的属性可以参考如下具体的代码
//Set Bold formatting to selected content
private void BtnBold_Click(object sender, RoutedEventArgs e)
{
if (MyRTB != null)
{
if (MyRTB.Selection.GetPropertyValue(Run.FontWeightProperty) is FontWeight && ((FontWeight)MyRTB.Selection.GetPropertyValue(Run.FontWeightProperty)) == FontWeights.Normal)
MyRTB.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold);
else
MyRTB.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Normal);
}
}
//<SnippetItalic>
//Set Italic formatting to selected content
private void BtnItalic_Click(object sender, RoutedEventArgs e)
{
if (MyRTB != null)
{
if (MyRTB.Selection.GetPropertyValue(Run.FontStyleProperty) is FontStyle && ((FontStyle)MyRTB.Selection.GetPropertyValue(Run.FontStyleProperty)) == FontStyles.Normal)
MyRTB.Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Italic);
else
MyRTB.Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Normal);
}
}
//Set Underline formatting to selected content
private void BtnUnderline_Click(object sender, RoutedEventArgs e)
{
if (MyRTB != null)
{
if (MyRTB.Selection.GetPropertyValue(Run.TextDecorationsProperty) == null)
MyRTB.Selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline);
else
MyRTB.Selection.ApplyPropertyValue(Run.TextDecorationsProperty, null);
}
}