I have a context menu but it is disappearing instantly after it shows up.
<TextBlock Name="InputtedAddress" Text="{Binding Path=InputtedAddress}" MouseDown="InputtedAddress_MouseDown"/>
System.Windows.Controls.ContextMenu thisMenu;
private void InputtedAddress_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.RightButton == MouseButtonState.Pressed)
{
thisMenu = new System.Windows.Controls.ContextMenu();
MenuItem thisMenuItem = new MenuItem() { Header = "Zoom to Incident" };
thisMenuItem.Click += new RoutedEventHandler(thisMenuItem_Click);
thisMenu.Items.Add(thisMenuItem);
thisMenu.IsOpen = true;
}
}
You should assign your menu to the ContextMenu property of your
TextBlockso that the opening and positioning will be taken care of for you. You also don’t need to create the menu in each MouseDown; just create it once and assign it to theContextMenuproperty.In XAML:
If you do want to show it manually you will need to position it before showing it by setting the PlacementTarget property, something like this:
P.S. “Inputted” is not a word 🙂