どうやら、で関数を使用すると、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() はどうなっていますか? ここで何か不足していますか?