dontcare
dontcare

Reputation: 975

Editable combobox overwrites my context menu

I implemented a editable ComboBox with XAML which should show my own context menu:

<ComboBox IsEditable="True"
          ContextMenu="{StaticResource contextMenu}"
          ContextMenuClosing="contextClosing">
    <ComboBoxItem Content="Item1" />
    <ComboBoxItem Content="Item2" />
</ComboBox>

but instead of showing my context menu, it shows the edit context menu (with Cut, Copy & Paste).

Is there a way to overwrite the editable context menu?

Upvotes: 1

Views: 1373

Answers (2)

anonymous
anonymous

Reputation: 1

2021 and it is still an issue! Excellent observation by dontcare, the combobox does have to be loaded to modify parts of it. However, rather than navigating the visual tree, you can access the textbox portion as;

private TextBox cmbTextBox 
{ 
   get { return GetTemplateChild("PART_EditableTextBox") as TextBox; }
}  

Upvotes: 0

dontcare
dontcare

Reputation: 975

The textbox of the editable part must be loaded to overwrite the standard context menu:

<ComboBox IsEditable="True" ContextMenuService.ShowOnDisabled="True" 
     Name="combobox" Loaded="combobox_Loaded">
     <ComboBox.ContextMenu>
         <ContextMenu>
            <MenuItem Header="test"></MenuItem>
         </ContextMenu>
     </ComboBox.ContextMenu>
     <ComboBoxItem Content="Item1"></ComboBoxItem>
     <ComboBoxItem Content="Item2"></ComboBoxItem>
</ComboBox>  

 private void combobox_Loaded(object sender, RoutedEventArgs e)
 {
   (VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(combobox, 0), 2) as TextBox).ContextMenu = combobox.ContextMenu;
 }  

Upvotes: 1

Related Questions