开发者

WPF RichTextBox - Selected Block?

开发者 https://www.devze.com 2022-12-25 01:09 出处:网络
I am experimenting with the WPF RichTextBox and notice that I can ittera开发者_如何转开发te through the blocks that make up its document by looping through RichTextBox.Document.Blocks.

I am experimenting with the WPF RichTextBox and notice that I can ittera开发者_如何转开发te through the blocks that make up its document by looping through RichTextBox.Document.Blocks.

What is the best way to get the Block that surrounds the caret?

I can get the CaretPosition and the ElementStart and ElementEnd properties of each block but can't see how to compare them because the actual character offsets are not exposed unless I am missing something obvious.


var curCaret = richTextBox1.CaretPosition;
var curBlock = richTextBox1.Document.Blocks.Where(x => x.ContentStart.CompareTo(curCaret) == -1 && x.ContentEnd.CompareTo(curCaret) == 1).FirstOrDefault();


Answer above probably works in WPF RTB but not in Silverlight 4.0. Most likely SL doesn't allow access to the Document protion of the RTB. So you have to do it via Reflection....

Something like this:

  • Set up a TextSelectionChanged Event Handler
  • Grab the TextSelection Pointer and find the Start TextPointer
  • Grab the TextSelection.Start.Parent item
  • Find out if it is of type paragraph
  • Parse the Paragraph.Inlines
  • Look for type of InlineUIContainer you need a cast it accordingly.


In Silverlight5 get the properties to be used to update a toolbar:

private void rtb_SelectionChanged(object sender, RoutedEventArgs e)
{
    TextSelection ts = rtb.Selection;
    object property;

    property =  ts.GetPropertyValue(Run.FontWeightProperty);
    System.Windows.FontWeight fontWeight = property is System.Windows.FontWeight ? (FontWeight)property : FontWeights.Normal;

    property = ts.GetPropertyValue(Run.FontStyleProperty);
    System.Windows.FontStyle fontStyle = property is System.Windows.FontStyle ? (FontStyle)property : FontStyles.Normal;

    TextDecorationCollection textDecorations = ts.GetPropertyValue(Run.TextDecorationsProperty) as TextDecorationCollection;
    bool isUnderlined = textDecorations != null;

    double? fontSize = ts.GetPropertyValue(Run.FontSizeProperty) as double?;
    SolidColorBrush foreground = ts.GetPropertyValue(Run.ForegroundProperty) as SolidColorBrush;
    Color foregroundColor = foreground != null ? foreground.Color : Colors.Black;

    System.Diagnostics.Debug.WriteLine("fontweight:{0}, fontStyle:{1}, Underline:{2}, size:{3}, color:{4}", 
        fontWeight,
        fontStyle,
        isUnderlined,
        fontSize, 
        foregroundColor);

    if (fontSize.HasValue)
        SetToolbarFontSize(fontSize.Value);

    SetToolbarFontColor(foregroundColor);
}


Paragraph currentParagraph = richTextBox1.CaretPosition.Paragraph;

This code will return a Paragaph object and instead of a Block object but because the blocks in a RichTextBox are typically paragraphs this will not pose any problem.

MS Docs:

The Blocks property is the content property of RichTextBox. It is a collection of Paragraph elements.

0

精彩评论

暂无评论...
验证码 换一张
取 消