Jim W.568372

I have a DataTemplate that includes a ComboBox. The ComboBox is set to IsEditable="true". I know this means that a textbox is included in the control tree. The problem is that my custom context menu is not propagated to the textbox portion of the control... it's on the button and the popup, but not the textbox. How do I work around this bug

e.g.:

Code Snippet

<DataTemplate x:Key="textbox" DataType="sx:XmlNode">

<ComboBox x:Name="combo" SelectedIndex="0" IsEditable="True" IsReadOnly="False" >

<ComboBox.ContextMenu>

<ContextMenu >

<MenuItem IsCheckable="True" Header="Another Something"/>

<MenuItem Header="Something" />

</ContextMenu>

</ComboBox.ContextMenu>

<ComboBoxItem>>(Not Keyed)</ComboBoxItem>

<ComboBoxItem>Unanswered</ComboBoxItem>

</ComboBox>

</DataTemplate>

Thanks!

-Jim W.



Re: Windows Presentation Foundation (WPF) ComboBox IsEditable="True" ContextMenu problem

Jim W.

Could I add a <Style/> in the ComboBox's Resources do do this If so, what would that look like



Re: Windows Presentation Foundation (WPF) ComboBox IsEditable="True" ContextMenu problem

Matt Eland

Style seems like the cleanest approach if you can get it working.

If you can't, you could do the brute force approach and grab Microsoft's default control template for the combo box, replicate it, and fix the text box portion. You can analyze the control template using StyleSnooper.

Analyzing the style is also helpful for figuring out what's not hooking up to what and give you other ideas for a fix.

Another way to do it would be to disable context menus on the entire combo box, but listen in code behind to mouse clicks and key presses to determine if the right mouse button or the context menu key were pressed and then at that point manually show your custom combo box.





Re: Windows Presentation Foundation (WPF) ComboBox IsEditable="True" ContextMenu problem

Jim W.

Unfortunately it looks like the <Style/> approach is problematic... I'd have to use a template of some sort to make it happen, and there's no support in the framework for ContextMenu templating.

(attempts to assign a <ContextMenu/> to a <Setter.Value/> fail because the <ContextMenu/> gets created as a shared instance resource, and can't be added to the tree again.)

So I guess I'll have to get medeval with the template if I'm going to make it work.





Re: Windows Presentation Foundation (WPF) ComboBox IsEditable="True" ContextMenu problem

Jim W.

To answer my own question...

The template given in the documentation for the combobox does not exhibit the problem with the context menu....

ms-help://MS.MSSDK.1033/MS.NETFX30SDK.1033/wpf_conceptual/html/b0662fa1-16d7-4320-b26b-c1804e565a44.htm

I just wish there'd been an easier way to take care of the problem.





Re: Windows Presentation Foundation (WPF) ComboBox IsEditable="True" ContextMenu problem

Jim W.

Jim W.568372 wrote:

To answer my own question...

The template given in the documentation for the combobox does not exhibit the problem with the context menu....

ms-help://MS.MSSDK.1033/MS.NETFX30SDK.1033/wpf_conceptual/html/b0662fa1-16d7-4320-b26b-c1804e565a44.htm

I just wish there'd been an easier way to take care of the problem.

Code Snippet

<TextBox x:Name="PART_EditableTextBox"

Style="{x:Null}"

Template="{StaticResource ComboBoxTextBox}"

HorizontalAlignment="Left"

VerticalAlignment="Center"

Margin="3,3,23,3"

Focusable="True"

Background="Transparent"

Visibility="Hidden"

IsReadOnly="{TemplateBinding IsReadOnly}"

ContextMenu="{TemplateBinding ComboBox.ContextMenu}"/><!-- Needed to add this binding -->