wpf 更改 RichTextBox 中选定文本的样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11874800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change style of selected Text in RichTextBox
提问by saber
How can I change styles (such as Font, FontSize, Brush) of selected Text in RichTextBox?
如何更改 中选定文本的样式(如字体、字体大小、画笔)RichTextBox?
Update :Let's say I've a RichTextBoxand a Toolbar. User comes and select text inside the RichTextBox box and change the font size from toolbar. I want to change style of selected text.
更新:假设我有RichTextBox一个工具栏。用户来并选择 RichTextBox 框中的文本并从工具栏更改字体大小。我想更改所选文本的样式。
回答by MethodMan
WPF
WPF
if (this.TextEditor.Selection.IsEmpty)
this.TextEditor.CurrentFontFamily = SelectedFont;
else
this.TextEditor.Selection.ApplyPropertyValue(TextElement.FontFamilyProperty, SelectedFont);
or another WPF Example
或另一个 WPF 示例
private void ChangeTextProperty(DependencyProperty dp, string value)
{
if (mainRTB == null) return;
TextSelection ts = richTextBox.Selection;
if (ts!=null)
ts.ApplyPropertyValue(dp, value);
richTextBox.Focus();
}
here are some examples Windows Changing the Font & Font Color (not wpf)
以下是一些示例 Windows 更改字体和字体颜色(不是 wpf)
richTextBox1.SelectionFont = new Font("Tahoma", 12, FontStyle.Bold);
richTextBox1.SelectionColor = System.Drawing.Color.Red;
another example below (not wpf)
下面的另一个例子(不是wpf)
private void WriteTextToRichTextBox()
{
// Clear all text from the RichTextBox;
richTextBox1.Clear();
// Set the font for the opening text to a larger Arial font;
richTextBox1.SelectionFont = new Font("Arial", 16);
// Assign the introduction text to the RichTextBox control.
richTextBox1.SelectedText = "The following is a list of bulleted items:" + "\n";
// Set the Font for the first item to a smaller size Arial font.
richTextBox1.SelectionFont = new Font("Arial", 12);
// Specify that the following items are to be added to a bulleted list.
richTextBox1.SelectionBullet = true;
// Set the color of the item text.
richTextBox1.SelectionColor = Color.Red;
// Assign the text to the bulleted item.
richTextBox1.SelectedText = "Apples" + "\n";
// Apply same font since font settings do not carry to next line.
richTextBox1.SelectionFont = new Font("Arial", 12);
richTextBox1.SelectionColor = Color.Orange;
richTextBox1.SelectedText = "Oranges" + "\n";
richTextBox1.SelectionFont = new Font("Arial", 12);
richTextBox1.SelectionColor = Color.Purple;
richTextBox1.SelectedText = "Grapes" + "\n";
// End the bulleted list.
richTextBox1.SelectionBullet = false;
// Specify the font size and string for text displayed below bulleted list.
richTextBox1.SelectionFont = new Font("Arial", 16);
richTextBox1.SelectedText = "Bulleted Text Complete!";
}
回答by Monroe Thomas
For a WPF RichTextBox, you have to use the ApplyPropertyValue method to a TextRange. You can get the selected TextRange using the Selected property of the RichTextBox instance.
对于 WPF RichTextBox,您必须对 TextRange 使用 ApplyPropertyValue 方法。您可以使用 RichTextBox 实例的 Selected 属性获取选定的 TextRange。
var selection = myRichTextBox.Selection;
if (!selection.IsEmpty)
selection.ApplyPropertyValue(TextElement.FontSizeProperty, 10.0);

