title: WPF RichTextBox SelectionChanged Performance
body: <p>I'm working on a word processor-type app using the WPF RichTextBox.  I'm using the SelectionChanged event to figure out what the font, font weight, style, etc. is of the current selection in the RTB using the following code:</p>
  
  <pre><code>private void richTextBox_SelectionChanged(object sender, RoutedEventArgs e)
      {
          TextSelection selection = richTextBox.Selection;
  
          if (selection.GetPropertyValue(FontFamilyProperty) != DependencyProperty.UnsetValue)
          {
              //we have a single font in the selection
              SelectionFontFamily = (FontFamily)selection.GetPropertyValue(FontFamilyProperty);
          }
          else
          {
              SelectionFontFamily = null;
          }
  
          if (selection.GetPropertyValue(FontWeightProperty) == DependencyProperty.UnsetValue)
          {
              SelectionIsBold = false;
          }
          else
          {
              SelectionIsBold = (FontWeights.Bold == ((FontWeight)selection.GetPropertyValue(FontWeightProperty)));
          }
  
          if (selection.GetPropertyValue(FontStyleProperty) == DependencyProperty.UnsetValue)
          {
              SelectionIsItalic = false;
          }
          else
          {
              SelectionIsItalic = (FontStyles.Italic == ((FontStyle)selection.GetPropertyValue(FontStyleProperty)));
          }
  
          if (selection.GetPropertyValue(Paragraph.TextAlignmentProperty) != DependencyProperty.UnsetValue)
          {
              SelectionIsLeftAligned = (TextAlignment)selection.GetPropertyValue(Paragraph.TextAlignmentProperty) == TextAlignment.Left;
              SelectionIsCenterAligned = (TextAlignment)selection.GetPropertyValue(Paragraph.TextAlignmentProperty) == TextAlignment.Center;
              SelectionIsRightAligned = (TextAlignment)selection.GetPropertyValue(Paragraph.TextAlignmentProperty) == TextAlignment.Right;
              SelectionIsJustified = (TextAlignment)selection.GetPropertyValue(Paragraph.TextAlignmentProperty) == TextAlignment.Justify;
          }            
      }
  </code></pre>
  
  <p>SelectionFontFamily, SelectionIsBold, etc. are each a DependencyProperty on the hosting UserControl with a Binding Mode of OneWayToSource.  They are bound to a ViewModel, which in turn has a View bound to it that has the Font combo box, bold, italic, underline, etc. controls on it.  When the selection in the RTB changes, those controls are also updated to reflect what's been selected.  This works great.</p>
  
  <p>Unfortunately, it works at the expense of performance, which is seriously impacted when selecting large amounts of text.  Selecting everything is noticeably slow, and then using something like Shift+Arrow Keys to change the selection is very slow.  Too slow to be acceptable.</p>
  
  <p>Am I doing something wrong?  Are there any suggestions on how to achieve reflecting the attributes of the selected text in the RTB to bound controls without killing the performance of the RTB in the process?  </p>