2

どうやら、で関数を使用すると、ApplyPropertyValue()正しくRichTextBox動作Selection.Selectしなくなります。テキストで満たされた RichTextBox があります。

string selectText(int index, int length)
        {
            TextRange textRange = new TextRange(TB.Document.ContentStart, TB.Document.ContentEnd);
            if (textRange.Text.Length >= (index + length))
            {
                TextPointer start = textRange.Start.GetPositionAtOffset(index, LogicalDirection.Forward);
                TextPointer end = textRange.Start.GetPositionAtOffset(index + length, LogicalDirection.Backward);
                TB.Selection.Select(start, end);
                return TB.Selection.Text;
            }
            return null;
        }
display.Text = selectText(6, 8);
// display contains the string "arranged"
TB.Selection.ApplyPropertyValue(RichTextBox.ForegroundProperty, "Red");
// display now contains the string "arrang" (should be "arranged")
display.Text = selectText(6, 8);

displayは、デバッグ目的で変数の値を表示するために使用される TextBlock でTBあり、RichTextBox です。

selectText(i,length)この関数は ApplyPropertyValue() なしで使用すると正常に動作し、常にテキストの正しい部分文字列を に返しますTB

上記のコードの実行後、display「arranged」ではなく「arrang」が含まれます。また、 ApplyPropertyValue() ステートメントを削除すると、display期待どおりに「配置」が含まれます。

ApplyPropertyValue() はどうなっていますか? ここで何か不足していますか?

4

0 に答える 0