Search code examples
c#wpfrichtextboxframeworkelement

How to get whether richtextbox last word is BringIntoView(shown) or not in WPF?


i am working in wpf,i have a richtextbox with some contents.if the contents are exceeded to richtextbox i want to hide bottom border.if the content within the richtextbox i want to show the bottom border.Now i am using the below code to bring the exceeded content to view in richtextbox.

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

But i want to display the bottom border,once the last word shown in that richtextbox.How to achieve this?

Note: I already known how to show bottom border dynamically.but i want to know last word are displayed within the richtextbox or not?

Regards Arjun


Solution

  • I can provide you with a method to check whether the upper left corner of a character is visible or not. Create a class library Tools with the following content:

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

    Note that PositionVisibleIs(...) is a static method and not dedicated to a specific object. In the code behind file of the window which contains your RichTextBox use the method like this:

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

    Hope this helps.