获取用户在TextBlock中单击的行号?

时间:2014-07-31 19:10:00

标签: c# xaml textblock

当用户点击时,是否可以获取文本块中文本行的行号? 假设用户单击文本块中的第3行,我可以捕获该信息吗?

<TextBlock x:Name="WorkspaceVersion" HorizontalAlignment="Left" Margin="879,45,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="703" Width="526" FontFamily="Consolas" FontSize="14">
        <TextBlock.Background>
            <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ControlDarkColorKey}}"/>
        </TextBlock.Background>

  // then the handler here
  WorkspaceVersion.MouseLeftButtonDown += new   MouseButtonEventHandler(WorkSpaceVersion_MouseLeftButtonDown);

  // handler
  private void WorkSpaceVersion_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        // can I caputer the line number of a click?
    }

我也愿意为此使用不同的控件。

1 个答案:

答案 0 :(得分:0)

好的,所以我通过获取TextBlock区域内的点击的X,Y坐标和按行高划分Y来解决这个问题。

private void WorkSpaceVersion_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
   Point position = e.GetPosition(WorkspaceVersion);
   var i = Math.Round(position.Y/15); // line height brah
}