如何从鼠标位置获取文本框的行?
How to get TextBox's line from mouse position?
我有一个包含多行文本的文本框,它正在像这样更新:
public void UpdateMessageBox(TextBox textBox, string text)
{
textBox.SelectionStart = 0;
textBox.SelectionLength = 0;
textBox.SelectedText = String.Format("{0:HH:mm:ss }", DateTime.Now) + text + "\n";
textBox.ScrollToHome();
}
现在我需要从立即单击鼠标中键的行中获取文本,而不是先通过左键单击选择行。
private void textBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Middle && e.ButtonState == MouseButtonState.Pressed)
{
e.MouseDevice.GetPosition(textBox) //what next?
}
}
如何从鼠标位置获取文本框行及其文本?
XAML
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBox Width="300"
Height="200"
PreviewMouseDown="TextBox_PreviewMouseDown"
Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut consectetur iaculis enim sed condimentum. Nunc vestibulum fermentum fermentum. Nam arcu ipsum, vestibulum eu felis a, varius gravida dolor. Pellentesque tempor cursus quam, mattis volutpat odio eleifend cursus. Morbi placerat auctor aliquam. Aliquam erat volutpat. Curabitur dictum convallis nibh in ullamcorper. "
TextWrapping="Wrap" />
<TextBlock x:Name="myTextBlock"
Grid.Row="1"
HorizontalAlignment="Center" />
</Grid>
代码隐藏
private void TextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Middle)
{
var myTextBox = (TextBox)sender;
var myCharIndex = myTextBox.GetCharacterIndexFromPoint(Mouse.GetPosition(myTextBox), true);
var myLineIndex = myTextBox.GetLineIndexFromCharacterIndex(myCharIndex);
var myLine = myTextBox.GetLineText(myLineIndex);
myTextBlock.Text = myLine;
}
}
我有一个包含多行文本的文本框,它正在像这样更新:
public void UpdateMessageBox(TextBox textBox, string text)
{
textBox.SelectionStart = 0;
textBox.SelectionLength = 0;
textBox.SelectedText = String.Format("{0:HH:mm:ss }", DateTime.Now) + text + "\n";
textBox.ScrollToHome();
}
现在我需要从立即单击鼠标中键的行中获取文本,而不是先通过左键单击选择行。
private void textBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Middle && e.ButtonState == MouseButtonState.Pressed)
{
e.MouseDevice.GetPosition(textBox) //what next?
}
}
如何从鼠标位置获取文本框行及其文本?
XAML
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBox Width="300"
Height="200"
PreviewMouseDown="TextBox_PreviewMouseDown"
Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut consectetur iaculis enim sed condimentum. Nunc vestibulum fermentum fermentum. Nam arcu ipsum, vestibulum eu felis a, varius gravida dolor. Pellentesque tempor cursus quam, mattis volutpat odio eleifend cursus. Morbi placerat auctor aliquam. Aliquam erat volutpat. Curabitur dictum convallis nibh in ullamcorper. "
TextWrapping="Wrap" />
<TextBlock x:Name="myTextBlock"
Grid.Row="1"
HorizontalAlignment="Center" />
</Grid>
代码隐藏
private void TextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Middle)
{
var myTextBox = (TextBox)sender;
var myCharIndex = myTextBox.GetCharacterIndexFromPoint(Mouse.GetPosition(myTextBox), true);
var myLineIndex = myTextBox.GetLineIndexFromCharacterIndex(myCharIndex);
var myLine = myTextBox.GetLineText(myLineIndex);
myTextBlock.Text = myLine;
}
}