【问题标题】:How to get whether richtextbox last word is BringIntoView(shown) or not in WPF?如何在WPF中获取richtextbox最后一个词是否为BringIntoView(显示)?
【发布时间】:2016-01-14 17:08:37
【问题描述】:

我在 wpf 中工作,我有一个包含一些内容的richtextbox。如果内容超出了richtextbox,我想隐藏底部边框。如果richtextbox 中的内容我想显示底部边框。现在我正在使用下面的代码可以在richtextbox中查看超出的内容。

 FrameworkContentElement fce = (startPos.Parent as FrameworkContentElement);
            if (fce != null)
            {
                fce.BringIntoView();
            }

但是我想显示底部边框,一旦最后一个单词显示在那个richtextbox中。如何实现这个?

注意:我已经知道如何动态显示底部边框。但我想知道最后一个单词是否显示在richtextbox中?

问候 阿琼

【问题讨论】:

    标签: c# wpf richtextbox frameworkelement


    【解决方案1】:

    我可以为您提供一种方法来检查字符的左上角是否可见。 创建类库Tools,内容如下:

    public class ToolsRtb
    {
        public static bool PositionVisibleIs(RichTextBox rtb, TextPointer pos)
        {
            // Rectangle around the character to check
            Rect r = pos.GetCharacterRect(LogicalDirection.Forward);
    
            // Upper left corner of the rectangle ...
            Point upperLeftCorner = r.Location;
    
            HitTestResult result = VisualTreeHelper.HitTest(rtb, upperLeftCorner);
    
            // ... is visible?
            if (result != null)
                return true;
            else
                return false;
        }   
    }
    

    请注意,PositionVisibleIs(...) 是一种静态方法,并非专用于特定对象。 在包含您的RichTextBox 的窗口的代码隐藏文件中,使用如下方法:

    // Is the last character of the current document visible?
    if (ToolsRtb.PositionVisibleIs(rtb, rtb.Document.ContentEnd) == true)
    {
        ...
    }
    

    希望这会有所帮助。

    【讨论】:

    • @ Pollitzer :它工作正常。但是在一次平移之后,我想清除当前内容并加载新内容,当时 Rect 值与以前相同。但我想清除它并表现得像与平移之前相同。我的意思是一旦结果值为 null,我在重新加载数据后进行一些平移,结果值也为 null。如何清除该值?
    • 清除RichTextBox 并加载新内容后,您必须再次调用 PositionVisibleIs(rtb, rtb.Document.ContentEnd) 因为最后可见位置会因内容而异,具体取决于字体大小,上大小写/小写、粗体/正常等
    • @Pollitzer:谢谢。但是 PositionVIsibles 来自哪个类?你能在代码中显示吗?
    • @Pollitzer:你能看看这个链接吗?你得到了我真正需要的东西。 [链接] (stackoverflow.com/questions/34783272/…)
    猜你喜欢
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多