ContextMenu StayOpen

Ribbon for WPF Forum

Posted 15 years ago by Krijn Michiels
Version: 4.5.0483
Avatar

How can I get this simple example to work ?

The contextMenu must stay open after CheckBox IsChecked state changes.

        <TextBox Width="250">
            <TextBox.ContextMenu>
                <ribbon:ContextMenu StaysOpen="True">
                    <ribbon:Menu>
                        <ribbon:CheckBox Label="Option1" 
                                         StaysOpenOnClick="True"/>
                        <ribbon:CheckBox Label="Option2" 
                                         StaysOpenOnClick="True"/>
                    </ribbon:Menu>
                </ribbon:ContextMenu>
            </TextBox.ContextMenu>
        </TextBox>

Comments (5)

Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Krijn,

I've been trying to debug this for well over an hour. We did have one bit of code that moved focus when the buttons were in a ContextMenu and that was causing the menu to close. However after I wrote some code to resolve that, I found it was still closing on clicks.

When I put a breakpoint on the ContextMenu.Closed event, all I can see is that the internal popup was closed but I'm not able to determine what exactly is triggering it. I put breakpoints in everything I can think of in our code that would move focus, and none of them hit, so it's likely something in Microsoft's code that is getting triggered. I'm not sure what else to try unfortunately.

Note that everything stays open fine if the root element is not a ContextMenu, so it works when in the ribbon. For some reason some internal MS ContextMenu code is causing this.

[Modified at 08/03/2009 03:16 PM]


Actipro Software Support

Posted 15 years ago by Krijn Michiels
Avatar
Quote:

Note that everything stays open fine if the root element is not a ContextMenu, so it works when in the ribbon. For some reason some internal MS ContextMenu code is causing this.


This is not correct. Default MS ContextMenu works fine :

 <TextBox Width="250">
            <TextBox.ContextMenu>
                <ContextMenu StaysOpen="True">
                    <MenuItem Header="MenuItem1"/>
                    <MenuItem Header="StaysOpen1"
                              StaysOpenOnClick="True"
                              IsCheckable="True" />
                    <MenuItem Header="StaysOpen2"
                              StaysOpenOnClick="True"
                              IsCheckable="True" />
                </ContextMenu>

            </TextBox.ContextMenu>
        </TextBox>
I've got it working by styling the default ContextMenu with the contenttemplate copied from the <ribbon:ContextMenu style using blend.

Resolution :

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ribbon="http://schemas.actiprosoftware.com/winfx/xaml/ribbon"
    xmlns:themes="http://schemas.actiprosoftware.com/winfx/xaml/themes"
    xmlns:shared="http://schemas.actiprosoftware.com/winfx/xaml/shared"
    Title="Window1" Height="300" Width="300">
    
    <Window.Resources>

        <Style x:Key="RibbonContextMenuItemStyle" TargetType="{x:Type MenuItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type MenuItem}">
                        <ribbon:MenuItem Content="{TemplateBinding Header}"
                                         ContentTemplate="{TemplateBinding HeaderTemplate}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

        </Style>

        <ControlTemplate x:Key="RibbonContextMenuTemplate" 
                         TargetType="{x:Type ContextMenu}">

            <shared:DropShadowChrome x:Name="DropShadow" 
                                             SnapsToDevicePixels="True" 
                                             Color="#00FFFFFF">
                <Border SnapsToDevicePixels="True" 
                                Background="{DynamicResource {ComponentResourceKey ResourceId=ContextMenuBackground, TypeInTargetAssembly={x:Type ribbon:Ribbon}}}" 
                                BorderBrush="{DynamicResource {ComponentResourceKey ResourceId=ContextMenuOuterBorder, TypeInTargetAssembly={x:Type ribbon:Ribbon}}}" 
                                BorderThickness="1,1,1,1">

                    <Border SnapsToDevicePixels="True" 
                                    BorderBrush="{DynamicResource {ComponentResourceKey ResourceId=ContextMenuInnerBorder, TypeInTargetAssembly={x:Type ribbon:Ribbon}}}" 
                                    BorderThickness="1,1,1,1">

                        <ScrollViewer Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type ribbon:Ribbon}}}" 
                                              CanContentScroll="True">

                            <ribbon:ScrollViewerItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                                                       Grid.IsSharedSizeScope="True" 
                                                                       KeyboardNavigation.DirectionalNavigation="Cycle" 
                                                                       KeyboardNavigation.TabNavigation="Cycle"/>

                        </ScrollViewer>

                    </Border>
                </Border>
            </shared:DropShadowChrome>
        </ControlTemplate>
        
        <Style TargetType="{x:Type ContextMenu}">
            <Setter Property="ItemContainerStyle" Value="{StaticResource RibbonContextMenuItemStyle}"/>
            <Setter Property="Template" Value="{StaticResource RibbonContextMenuTemplate}"/>
            <Style.Resources>
                <Style TargetType="{x:Type ribbon:Button}">
                    <Setter Property="Context" Value="MenuItem"/>
                </Style>
                <Style TargetType="{x:Type ribbon:PopupButton}">
                    <Setter Property="Context" Value="MenuItem"/>
                </Style>
                <Style TargetType="{x:Type ribbon:CheckBox}">
                    <Setter Property="Focusable" Value="False"/>
                    <Setter Property="Context" Value="MenuItem"/>
                </Style>
            </Style.Resources>
        </Style>

    </Window.Resources>
    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        
        <TextBox Width="250">
            <TextBox.ContextMenu>
                <ContextMenu StaysOpen="True">
                    
                    <ribbon:Button Label="test" />
                    
                    <ribbon:PopupButton Label="SubMenu" Context="MenuItem">
                        <ribbon:Menu>
                            <ribbon:Button Label="SubOption1"/>
                            <ribbon:Button Label="SubOption2"/>
                            <ribbon:CheckBox Label="SubOption StayOpen" Focusable="False" StaysOpenOnClick="True"/>
                            <ribbon:CheckBox Label="SubOption CloseOnCheck"/>
                        </ribbon:Menu>    
                    </ribbon:PopupButton>

                    <ribbon:CheckBox Label="Option1"
                                     StaysOpenOnClick="True"/>

                    <ribbon:CheckBox Label="Option2" 
                                     StaysOpenOnClick="True"/>

                </ContextMenu>

            </TextBox.ContextMenu>
        </TextBox>
        
    </Grid>
</Window>

Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Good news... I did some more investigating and found what was causing it. It's fixed for the next maintenance release.


Actipro Software Support

Posted 12 years ago by Parth
Avatar

I am facing the same problem, though I am using the latest verison. 

In the ribbon popup, I tried to put some configuration selections including checkboxes and text boxes, 

but after I click over checkbox, popup is getting closes on every click. Do you any other way to handle just to use onfocus and off focus,

or any particular button inside the ribbon:PopupButton do the job for closing popup window, not others. 

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Parth,

If you are having troubles, please make a new very simple sample project that just shows this issue and e-mail it to our support address.  Then we can debug it.  Please rename the .zip file extension so it doesn't get spam blocked.


Actipro Software Support

The latest build of this product (v24.1.1) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.