当您使用RichTextBox的FontFamily属性时,它将更改FlowDocument中整个内容的FontFamily。 同样的方法,你可以执行一个像EditingCommands.ToggleBold这样的命令,它只改变插入符号下的单词或者只是写入新的文本,应该有一种方法可以对FontsFamilies和Color做同样的事情。如何在不修改以前的文本的情况下更改WPF RichTextBox中的FontFamily
1
A
回答
3
也许不是最巧妙的解决方案,但你可以从RichTextBox的继承并添加一些行为
声明自己的字体属性,因此您可以用字体
的列表public class CustomControl1 : RichTextBox
{
public static readonly DependencyProperty CurrentFontFamilyProperty =
DependencyProperty.Register("CurrentFontFamily", typeof(FontFamily), typeof (CustomControl1), new FrameworkPropertyMetadata(new FontFamily("Tahoma"), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,new PropertyChangedCallback(OnCurrentFontChanged)));
}
覆盖后来将它们绑定OnTextInput。如果您CurrentFontProperty改变获得插入符的位置,你可以不同意在RichTextBox中还内置了用于处理KeyDown和KEYUP鼓泡和它们之间的的TextInput此事件产生
protected override void OnTextInput(TextCompositionEventArgs e)
{
if (fontchanged)
{
TextPointer tp = this.CaretPosition.GetInsertionPosition(LogicalDirection.Forward);
Run r = new Run(e.Text, tp);
r.FontFamily = CurrentFontFamily;
r.Foreground = CurrentForeground;
this.CaretPosition = r.ElementEnd;
fontchanged = false;
}
else
base.OnTextInput(e);
}
,并创建一个新的运行新的文本输入并设置FontFamily = CurrentFontFamily。你也可以改变整个单词,如果卡尔特是一个字,这篇文章可能会发现有趣的单词Navigate Words in RichTextBox。
1
你会使用一个RUN RichTextBox的,像内:
<RichTextBox>
<Run FontFamily="Arial">My Arial Content</Run>
<Run FontFamily="Times" FontWeight="Bold">My bolded Times content</Run>
<Run>My Content that inherits Font From the RTB</Run>
</RichTextBox>
好吧,这得到了一些低级别的斗hickies玩。但是,我们去:
首先,将一些ToggleButtons和一个RichTextBox添加到XAML表单。在富文本框中,您会给它一些命令绑定,以便让系统知道一切正常。
这里的XAML:
<RichTextBox Height="119" Name="RichTextBox1" Width="254" >
<RichTextBox.CommandBindings>
<CommandBinding Command="EditingCommands.ToggleBold" CanExecute="CommandBinding_CanExecute" ></CommandBinding>
<CommandBinding Command="EditingCommands.ToggleItalic" CanExecute="CommandBinding_CanExecute" ></CommandBinding>
</RichTextBox.CommandBindings>
</RichTextBox>
<ToggleButton MinWidth="40" Command="EditingCommands.ToggleBold" Height="23" HorizontalAlignment="Left" Name="Button1" VerticalAlignment="Top" Width="75" CommandTarget="{Binding ElementName=RichTextBox1}" >Bold</ToggleButton>
<ToggleButton MinWidth="40" Command="EditingCommands.ToggleBold" Height="23" HorizontalAlignment="Left" Name="Button2" VerticalAlignment="Top" Width="75" CommandTarget="{Binding ElementName=RichTextBox1}" >Italics</ToggleButton>
现在,那里的东西是一个RichTextBox和两个切换按钮,和的ToggleButtons都跟化CommandBindings到ToggleBold/ToggleItalics独立。
在代码方面,我有两种方法:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
End Sub
Private Sub CommandBinding_CanExecute(ByVal sender As System.Object, ByVal e As System.Windows.Input.CanExecuteRoutedEventArgs)
e.CanExecute = True
End Sub
按钮单击事件处理程序是因为有一个按钮,需要事件处理程序可以使用。
CanExecute告诉按钮该值是否可用于加粗(例如,您可以检查长度,如果RTB为空时不尝试加粗)。
现在,对于真正低层次的事物控制,您将不得不在RichTextFormat中进行操作。按照此link了解更多。
2
这是我用来避免覆盖richtextbox。这允许您改变选择的样式(如果有),或者在插入符号后更改“要添加”文本的样式。希望能帮助到你。
public static void SetFontSize(RichTextBox target, double value)
{
// Make sure we have a richtextbox.
if (target == null)
return;
// Make sure we have a selection. Should have one even if there is no text selected.
if (target.Selection != null)
{
// Check whether there is text selected or just sitting at cursor
if (target.Selection.IsEmpty)
{
// Check to see if we are at the start of the textbox and nothing has been added yet
if (target.Selection.Start.Paragraph == null)
{
// Add a new paragraph object to the richtextbox with the fontsize
Paragraph p = new Paragraph();
p.FontSize = value;
target.Document.Blocks.Add(p);
}
else
{
// Get current position of cursor
TextPointer curCaret = target.CaretPosition;
// Get the current block object that the cursor is in
Block curBlock = target.Document.Blocks.Where
(x => x.ContentStart.CompareTo(curCaret) == -1 && x.ContentEnd.CompareTo(curCaret) == 1).FirstOrDefault();
if (curBlock != null)
{
Paragraph curParagraph = curBlock as Paragraph;
// Create a new run object with the fontsize, and add it to the current block
Run newRun = new Run();
newRun.FontSize = value;
curParagraph.Inlines.Add(newRun);
// Reset the cursor into the new block.
// If we don't do this, the font size will default again when you start typing.
target.CaretPosition = newRun.ElementStart;
}
}
}
else // There is selected text, so change the fontsize of the selection
{
TextRange selectionTextRange = new TextRange(target.Selection.Start, target.Selection.End);
selectionTextRange.ApplyPropertyValue(TextElement.FontSizeProperty, value);
}
}
// Reset the focus onto the richtextbox after selecting the font in a toolbar etc
target.Focus();
}
相关问题
- 1. 如何在不更改样式的情况下设置RichTextBox.SelectionFont FontFamily?
- 2. 如何在不更改样式的情况下设置RichTextBox.SelectionFont FontFamily?
- 3. 是否可以在不更改散列值的情况下修改文件
- 4. 如何在不修改.html的情况下以纯文本形式显示html
- 5. 如何在不更改当前内容的情况下更改NSTextView的字体?
- 6. 在不更改版本的情况下更新受管文档
- 7. 如何在不更改代码的情况下修改wordpress主题?
- 8. 如何在不修改时间的情况下仅更改时区
- 9. 如何在不覆盖当前更改的情况下将文件恢复到以前的版本?
- 10. 如何在不更改文件类型的情况下更改文件名?
- 11. 在不改变背景风格的情况下更改jumbotron中的文本
- 12. 更改所选值上情况下,在RichTextBox中在VB 2008
- 13. 如何在不中断PHP的情况下更改我的DocumentRoot?
- 14. 是否可以在richtextbox中更改文本的位置?如何?
- 15. 在不修改旧数据的情况下更新数据
- 16. 如何在不添加填充的情况下修改标题?
- 17. 如何在不更改当前工作目录的情况下添加文件?
- 18. CKEditor:在不改变数据的情况下修改视图
- 19. 如何在不拉伸文本的情况下更改TextField的宽度?
- 20. 如何在不点击listview委托的情况下更改按钮的文本?
- 21. 我该如何修改这个程序,以便在不修改文件的情况下计算散列?
- 22. WPF RichTextBox:如何更改选定的文本字体?
- 23. 如何在不更改凭据的情况下更新用户
- 24. 如何在不更改rowversion的情况下更新记录
- 25. 在不更改数据库的情况下更改模型?
- 26. 在不更改模式的情况下更改班级
- 27. 在不重建JAR的情况下修改JAR文件
- 28. Mercurial:在不修改文件的情况下移动书签?
- 29. 在没有版本更改的情况下检测不兼容的API更改
- 30. 如何更改WPF中的上下文
我想改变它dinamically,以及命令。这对于静态文本很好。但RichTextBox中的poing通过键盘输入可以产生具有不同Text属性的文本。 – jmayor 2009-07-17 14:51:06
然后看看RTF,并读/写它。 http://www.devcity.net/PrintArticle.aspx?ArticleID=356 – 2009-07-17 15:37:22
问题是,第一个和所有以下内容将从它们的容器继承textElement属性(可能是RichTextEditor背后的FlowDocument),所以当更改编辑器的字体时,它会更改引用它的所有元素的字体。 到目前为止,我可以通过在更改字体时生成新的运行标记来下载PreviewKey来管理该问题,但之后我必须过滤键盘输入。我确信有更好的方法来做到这一点,所有的EditingCommnads都可以用粗体,斜体,fontSize来做同样的工作,当然也可以用一种奇特的方式来做。 –
jmayor
2009-07-17 18:17:38