我一直无法找到答案。我正在使用在滚动查看器中显示的TextBlock。 TextWrapping设置为wrap:
<ScrollViewer VerticalScrollBarVisibility="Auto"
Width="{Binding Parent.ActualWidth, Mode=OneWay, RelativeSource={RelativeSource Self}}"
Height="{Binding Parent.ActualHeight, Mode=OneWay, RelativeSource={RelativeSource Self}}"
Name="theScrollViewer">
<Grid Background="White" >
<wc:CustomTextBlock
InLineText="{Binding Text, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
TextWrapping="Wrap"
WordPad="{Binding WordPad, UpdateSourceTrigger=PropertyChanged, Mode=OneWayToSource}"
/>
TextBlock由内联元素填充,每个元素包含一个由以下定义的逻辑单词:
Regex.Split(text, @"(\s+)")
现在,在Inline元素的MouseEnter事件中,我正在创建一个 边界矩形:
static void inline_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
Run Sender = sender as Run;
TextPointer tp0 = Sender.ContentStart;
TextPointer tp1 = Sender.ContentEnd;
Rect StartRect = tp0.GetCharacterRect(LogicalDirection.Forward);
Rect EndRect = tp1.GetCharacterRect(LogicalDirection.Backward);
StartRect.Union(EndRect);
.........................
}
当内联元素被包装到TextBlock的下一行时,这是明显失败的。
当内联元素被包裹时,如何找到它的显示位置&#34;到下一行。我正在寻找一种生产两个矩形的方法:
矩形在行尾的内联的第一部分周围,
在下一行开头的内联结尾部分周围的矩形。
我非常感谢任何帮助,因为我是flowdocuments的新手。感谢。
答案 0 :(得分:0)
作为一个新手,有一个大师评论肯定会很高兴。与此同时,我发现这在我的情况下正常工作:
static void inline_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
Run Sender = sender as Run;
TextPointer tp0 = Sender.ElementStart;
TextPointer tp1 = Sender.ElementEnd;
Rect r0 = tp0.GetCharacterRect(LogicalDirection.Forward);
Rect r1 = tp1.GetCharacterRect(LogicalDirection.Backward);
if (r1.Top != r0.Top)
{
/* this inline element spans two physical rows */
Rect StartRect0 = r0;
Rect EndRect0 = tp1.GetLineStartPosition(0).GetCharacterRect(LogicalDirection.Backward);
StartRect0.Union(EndRect0);
Rect StartRect1 = new Rect(0, r1.Top, 0, r1.Height);
Rect EndRect1 = r1;
StartRect1.Union(EndRect1);
}
else
{
Rect StartRect0 = tp0.GetCharacterRect(LogicalDirection.Forward);
Rect EndRect0 = tp1.GetCharacterRect(LogicalDirection.Backward);
StartRect0.Union(EndRect0);
}
}
希望这有助于某人。