Adorner in textbox

Naveen kumar 1 Reputation point
2020-07-13T10:03:03.173+00:00

I have created an adornerlayer over wpf textbox.

This code block will return the bounds of the text in textbox, i have written this block in onrender method of a adorner class.

Rect startPosition = textBox.GetRectFromCharacterIndex(startingCharOfAWord);
Rect endPosition = textBox.GetRectFromCharacterIndex(endingCharOfAWord,true);
Rect rectUnion = Rect.Union(startPosition, endPosition);

using this(rectPosition) position i can highlight the text in the textbox.

My Question : My textbox has more than 500 words. I need to scroll to read the text in the textbox. while scrolling in it, i used PreviewMouseWheel Event to update the adorner layer. But Rect returns empty. So i can't able get the bounds of the text.

Suggest me an idea to get Rect value of the text when scrolling the text in the texbox.

    public partial class MainWindow : Window
    {
        AdornErrorText adornErrorText;
        AdornerLayer adornerLayer;
        internal Dictionary<int, object> wordList = new Dictionary<int, object>();
        public MainWindow()
        {
            InitializeComponent();

        }
        internal void SetRedUnderline(string text)
        {
            string[] words = text.Split(' ');
            int index = 0;
            foreach(string word in words)
            {
                wordList.Add(index, word);
                index =index + word.Length + 1;
            }
        }
        private void MyButton_Click(object sender, RoutedEventArgs e)
        {
            if (adornerLayer == null)
            {
                adornerLayer = AdornerLayer.GetAdornerLayer(MyTextBox);
                adornErrorText = new AdornErrorText(MyTextBox, this);
                adornerLayer.Add(adornErrorText);
            }
            MyTextBox.PreviewMouseWheel += MyButton_PreviewMouseWheel;
        }
        private void MyButton_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            adornerLayer.Update(MyTextBox);
        }
    }
    internal class AdornErrorText : Adorner
    {
        private MainWindow mainWindow;
        internal AdornErrorText(UIElement targetElement, MainWindow mainWindowInstance) : base(targetElement)
        {
            mainWindow = mainWindowInstance;
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            TextBox textBox = this.AdornedElement as TextBox;
            mainWindow.wordList.Clear();
            mainWindow.SetRedUnderline(textBox.Text);

            drawingContext.PushClip(new RectangleGeometry(new Rect(0, 0, this.AdornedElement.RenderSize.Width, this.AdornedElement.RenderSize.Height)));

            foreach (KeyValuePair<int, object> keyValuePair in mainWindow.wordList)
            {
                Rect startPosition = textBox.GetRectFromCharacterIndex(keyValuePair.Key);
                Rect endPosition = textBox.GetRectFromCharacterIndex(keyValuePair.Key + keyValuePair.Value.ToString().Length - 1, true);
                Rect rectUnion = Rect.Union(startPosition, endPosition);

                drawingContext.DrawLine(new Pen(Brushes.Red,1), rectUnion.BottomLeft, rectUnion.BottomRight);

            }
            drawingContext.Pop();
        }
    }
}
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.