Specifically, what I'd like is to have a user right-click in a TextBox, figure out and save the index position within the text where the right-click occurred, and then later insert some text at that position once the user makes a choice from the context menu that pops up because of the right click.
The tricky part is getting the index position based on the coordinates of the right-click.
This is in Silverlight 4.
private int _insertPoint;
private void userNotification_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
// Move and open the context menu relative to its container.
contextMenu.HorizontalOffset = e.GetPosition(container).X;
contextMenu.VerticalOffset = e.GetPosition(container).Y;
contextMenu.IsOpen = true;
// Get the click coordinates relative to the TextBox.
int clickX = e.GetPosition(textBox).X;
int clickY = e.GetPosition(textBox).Y;
_insertPoint = ?; // Here's the problem.
}
private void SelectFieldToInsert(object sender, MouseButtonEventArgs e)
{
// Close the context menu.
contextMenu.IsOpen = false;
var item = sender as ListBoxItem;
textBox.Text = textBox.Text.Insert(_insertPoint, "<" + item.Content + ">");
}