WPF RichTextBox - 选定的块?

发布于 2024-08-27 23:28:05 字数 214 浏览 2 评论 0原文

我正在尝试 WPF RichTextBox,并注意到我可以通过循环 RichTextBox.Document.Blocks 来遍历构成其文档的块。

获得插入符周围的块的最佳方法是什么?

我可以获得每个块的 CaretPosition 以及 ElementStart 和 ElementEnd 属性,但看不到如何比较它们,因为除非我遗漏了一些明显的东西,否则实际的字符偏移量不会公开。

I am experimenting with the WPF RichTextBox and notice that I can itterate 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别再吹冷风 2024-09-03 23:28:05
var curCaret = richTextBox1.CaretPosition;
var curBlock = richTextBox1.Document.Blocks.Where(x => x.ContentStart.CompareTo(curCaret) == -1 && x.ContentEnd.CompareTo(curCaret) == 1).FirstOrDefault();
var curCaret = richTextBox1.CaretPosition;
var curBlock = richTextBox1.Document.Blocks.Where(x => x.ContentStart.CompareTo(curCaret) == -1 && x.ContentEnd.CompareTo(curCaret) == 1).FirstOrDefault();
过去的过去 2024-09-03 23:28:05

上面的答案可能适用于 WPF RTB,但不适用于 Silverlight 4.0。 SL 很可能不允许访问 RTB 的文档部分。所以你必须通过反射来做到这一点......

像这样:

  • 设置一个 TextSelectionChanged 事件处理程序
  • 抓住 TextSelection 指针并找到 Start TextPointer
  • 抓住 TextSelection.Start.Parent 项目
  • 找出它是否是
  • 段落 类型 解析段落.Inlines
  • 查找 InlineUIContainer 的类型,您需要相应地对其进行强制转换。

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.
Saygoodbye 2024-09-03 23:28:05

在 Silverlight5 中获取用于更新工具栏的属性:

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);
}

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);
}
执妄 2024-09-03 23:28:05
Paragraph currentParagraph = richTextBox1.CaretPosition.Paragraph;

此代码将返回 Paragaph 对象而不是 Block 对象,但由于 RichTextBox 中的块通常是段落,因此不会造成任何问题。

MS文档:

Blocks 属性是 RichTextBox 的内容属性。它是 Paragraph 元素的集合。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文