private static void OnFormatBarPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            global::System.Windows.Controls.RichTextBox rtb = d as global::System.Windows.Controls.RichTextBox;
            if (rtb == null)
            {
                throw new Exception("A FormatBar can only be applied to a RichTextBox.");
            }

            RichTextBoxFormatBarManager manager = new RichTextBoxFormatBarManager();

            manager.AttachFormatBarToRichtextBox(rtb, e.NewValue as IRichTextBoxFormatBar);
        }
        /// <summary>
        /// Attaches a FormatBar to a RichtextBox
        /// </summary>
        /// <param name="richTextBox">The RichtextBox to attach to.</param>
        /// <param name="formatBar">The Formatbar to attach.</param>
        private void AttachFormatBarToRichtextBox(global::System.Windows.Controls.RichTextBox richTextBox, IRichTextBoxFormatBar formatBar)
        {
            _richTextBox = richTextBox;
            //we cannot use the PreviewMouseLeftButtonUp event because of selection bugs.
            //we cannot use the MouseLeftButtonUp event because it is handled by the RichTextBox and does not bubble up to here, so we must
            //add a hander to the MouseUpEvent using the Addhandler syntax, and specify to listen for handled events too.
            _richTextBox.AddHandler(Mouse.MouseUpEvent, new MouseButtonEventHandler(RichTextBox_MouseButtonUp), true);
            _richTextBox.TextChanged += RichTextBox_TextChanged;

            _adorner = new UIElementAdorner <Control>(_richTextBox);

            formatBar.Target = _richTextBox;
            _toolbar         = formatBar;
        }
    /// <summary>
    /// Attaches a FormatBar to a RichtextBox
    /// </summary>
    /// <param name="richTextBox">The RichtextBox to attach to.</param>
    /// <param name="formatBar">The Formatbar to attach.</param>
    private void AttachFormatBarToRichtextBox( global::System.Windows.Controls.RichTextBox richTextBox, IRichTextBoxFormatBar formatBar )
    {
      _richTextBox = richTextBox;
      _richTextBox.PreviewMouseMove += RichTextBox_PreviewMouseMove;
      //we cannot use the PreviewMouseLeftButtonUp event because of selection bugs.
      //we cannot use the MouseLeftButtonUp event because it is handled by the RichTextBox and does not bubble up to here, so we must
      //add a hander to the MouseUpEvent using the Addhandler syntax, and specify to listen for handled events too.
      _richTextBox.AddHandler( Mouse.MouseUpEvent, new MouseButtonEventHandler( RichTextBox_MouseButtonUp ), true );
      _richTextBox.TextChanged += RichTextBox_TextChanged;

      _adorner = new UIElementAdorner<Control>( _richTextBox );

      formatBar.Target = _richTextBox;
      _toolbar = formatBar;
    }