2

WPF の textBox 内のテキストの位置を表す rect を取得しようとしています。

私が抱えている問題は、四角形に返される値が「無限大」として返されることです。

rect.X は本当に私が気にするすべてです。

たとえば、このメソッドを textChanged イベントで呼び出すと、機能します。

TextBox.Loaded イベント内でこのメソッドを呼び出すと、上記が返されます。私が見逃している可能性のあるものについての洞察を探しています。

レイアウトがまだ計算されていないことは理解していますが、TextBox の LayoutUpdated() イベントにアタッチしても機能しません。

何かが表示される場合のコード:

foreach (var word in rslt)
      {
        //find the index of the start of the invalid word
        int idx1 = tbx.Text.ToLower().IndexOf(word.ToLower());
        //find the index of the end of the invalid word
        int idx2 = idx1 + (word.Length);

        if (idx1 == -1)
          continue;

        //get a rect defining the location in coordinates of the invalid word.
        var rec1 = tbx.GetRectFromCharacterIndex(idx1);
        var rec2 = tbx.GetRectFromCharacterIndex(idx2);

        //if the word is not visible or not fully visible, do not show the red line.
        if (tbx.ActualWidth > 0)
        {
          if (rec1.X < 0 || rec2.X > tbx.ActualWidth)
            continue;
        }

        if (rec1.Y < 0)
          continue;

        //actually draw the line under the word
        SquigglyAdorner ado = new SquigglyAdorner(tbx, word, rec1.X, rec2.X, rec2.Bottom);
        adornerLayer.Add(ado);
      }
    };
4

0 に答える 0