2009-07-17 62 views

回答

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了解更多。

+0

我想改变它dinamically,以及命令。这对于静态文本很好。但RichTextBox中的poing通过键盘输入可以产生具有不同Text属性的文本。 – jmayor 2009-07-17 14:51:06

+0

然后看看RTF,并读/写它。 http://www.devcity.net/PrintArticle.aspx?ArticleID=356 – 2009-07-17 15:37:22

+0

问题是,第一个和所有以下内容将从它们的容器继承textElement属性(可能是RichTextEditor背后的FlowDocument),所以当更改编辑器的字体时,它会更改引用它的所有元素的字体。 到目前为止,我可以通过在更改字体时生成新的运行标记来下载PreviewKey来管理该问题,但之后我必须过滤键盘输入。我确信有更好的方法来做到这一点,所有的EditingCommnads都可以用粗体,斜体,fontSize来做同样的工作,当然也可以用一种奇特的方式来做。 – jmayor 2009-07-17 18:17:38

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(); 
    } 
相关问题