I created a ContextMenu with several MenuItems. When the user clicks on a Label the ContextMenu disappear but I want to keep it Opened. The aims is to avoid the reopen the menu when the user miss click a textbox or a checkbox.
The XAML code is :
<ContextMenu
x:Key="contextMenuListeNew"
Name="contextMenuUser"
StaysOpen="True"
>
<MenuItem
Header="Justifier"
Name="contextMenuJustifier"
StaysOpenOnClick="True"
>
</MenuItem>
<MenuItem
Header="Corriger"
Name="MenuItemModifier"
StaysOpenOnClick="True"
IsSubmenuOpen="True"
MouseLeftButtonDown="MenuItem_MouseLeftButtonDown"
>
<StackPanel
Orientation="Horizontal"
Margin="2,0,0,0"
MouseLeftButtonDown="MenuItem_MouseLeftButtonDown"
>
<Label
Name="LabelDispo"
Content="Dispo"
MouseLeftButtonDown="MenuItem_MouseLeftButtonDown"
/>
<TextBox
Name="TextBoxDispoBrute"
Text=""
VerticalContentAlignment="Center"
MouseLeftButtonDown="MenuItem_MouseLeftButtonDown"
Width="60"
/>
<Label
Name="LabelPourcentage"
MouseLeftButtonDown="MenuItem_MouseLeftButtonDown"
Content="%" />
<CheckBox
Name="CheckBoxAllCells"
Margin="0,5,0,0"
MouseLeftButtonDown="MenuItem_MouseLeftButtonDown"
Content="dont les 100%"
/>
</StackPanel>
<StackPanel
Orientation="Horizontal"
MouseLeftButtonDown="MenuItem_MouseLeftButtonDown"
Margin="0,5,0,0">
<Label
Content="Commentaire"
MouseLeftButtonDown="MenuItem_MouseLeftButtonDown"/>
<TextBox
Name="TextBoxCommentaireDispo"
Height="25"
MouseLeftButtonDown="MenuItem_MouseLeftButtonDown"
Width="135"
VerticalContentAlignment="Center"
Text=""
/>
</StackPanel>
<Separator />
<Button
Content="OK"
MouseLeftButtonDown="MenuItem_MouseLeftButtonDown"
Name="ButtonValiderDispo"
Width="80"
Height="20"
/>
</MenuItem>
<MenuItem
Header="RéInitialiser"
Name="MenuItemReset"
StaysOpenOnClick="True"
>
</MenuItem>
</ContextMenu>
It looks like something like that ContextMenu
So when the user click on "Dispo" or "Commentaire" the contextmenu will close.
With the property StaysOpenOnClick="True" the MenuItem stays open only if its contains only a Name to click.
So "Justifier" and Réinitialiser" stays open thanks to this property. But it does not works when a add a stack panel with several elements like in the item "Corriger".
I found a first kind of workaround with the method :
Click="MenuItem_Click" that is placed on the MenuItem with this implementation :
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
(e.OriginalSource as MenuItem).StaysOpenOnClick = true;
(e.OriginalSource as MenuItem).IsSubmenuOpen = true;
(e.Source as MenuItem).StaysOpenOnClick = true;
(e.Source as MenuItem).IsSubmenuOpen = true;
}
With this code the ContextMenu stays open only after the second click. On the first One the ContextMenu steal close but after it stays opened.
Do you know how to do it even at the first Click ?
Best Regards,