You could try to add con.focusable = false;
so that the string is still selected when you right-click to display the context menu.
The code of xaml:
<Grid>
<TextBox x:Name="myText" Height="38" Margin="0,72,6,0" VerticalAlignment="Top" HorizontalAlignment="Right" Width="135"></TextBox>
<Label Height="30" Margin="12,80,164,0" Name="label1" VerticalAlignment="Top">Textbox with contextMenu property set</Label>
</Grid>
The code of xaml.cs:
public partial class MainWindow : Window
{
ContextMenu con = new ContextMenu();
public MainWindow()
{
InitializeComponent();
MenuItem menuItem1 = new MenuItem();
menuItem1.Header = "Menu1";
MenuItem menuItem2 = new MenuItem();
menuItem2.Header = "Menu2";
con.Items.Add(menuItem1);
con.Items.Add(menuItem2);
con.Focusable = false;
this.myText.ContextMenu = con;
}
}
The picture of result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our [documentation][3] to enable e-mail notifications if you want to receive the related email notification for this thread.
[3]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html