I had a similar problem, but I generated my controls programmatically, and my parent control is a dock. Based on the accepted answer, I decided to set the value to zero in the code.
<Grid> <DockPanel> <TextBox Name="txtBox" VerticalAlignment="Top" HorizontalAlignment="Left" Width="50"></TextBox> </DockPanel> </Grid>
and then
private void Window_Loaded(object sender, RoutedEventArgs e) { txtBox.ContextMenu = null; }
EDIT: I felt this was a kind of random answer, as it does not completely or directly solve this issue. I did something, and if you implement the method found in the answer to this question , you can find the text field in the code.
So, if you have this
<Grid> <ContentControl> <ContentControl.ContentTemplate> <DataTemplate> <TextBox Name="txtBox" VerticalAlignment="Top" HorizontalAlignment="Left" Width="50"></TextBox> </DataTemplate> </ContentControl.ContentTemplate> </ContentControl> </Grid>
Then you can find your text field by name (txtBox in this case) and set the context menu to null
TextBox myTextBox = FindChild<TextBox>(Application.Current.MainWindow, "txtBox"); myTextBox.ContextMenu = null;
Personally, I would prefer this to create a new class with inheritance, but that's all that works for you. It still doesn't answer "Why is this happening?" but I think the accepted answer does a good job of this.
Joe j
source share