WPF sub menuitems closes before mouse reach them

Viewed 207

A very few people have this problem that they cannot react the sub menuitems because it closes too fast before the mouse can reach the sub menu. Added a GIF at the bottom. We cannot reproduce this behaviour and it seems to affect very few people. enter image description here

We use Hardcodet.Wpf.TaskbarIcon to display the menu. Any ideas would be appreciated. Here is a snippet of the code where I illustrated only 1 menu item but the others follow the same logic:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:tb="http://www.hardcodet.net/taskbar"
                    xmlns:local="clr-namespace:FreedomPlatform"
                    xmlns:converters="clr-namespace:FreedomPlatform.Converters">
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    <converters:InverseBooleanConverter x:Key="InverseBooleanConverter" />
    <converters:InverseAndBooleanConverter x:Key="InverseAndBooleanConverter" />
    <Style TargetType="{x:Type MenuItem}">
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
    </Style>
    <ContextMenu x:Key="FreedomTrayMenu" AutomationProperties.Name="Freedom">
        
        <MenuItem Header="Options">
            <MenuItem Header="Run on Startup" StaysOpenOnClick="True" IsCheckable="True" IsChecked="{Binding RunOnStartup}" IsEnabled="{Binding RunOnStartupModificationEnabled}" />
            
        </MenuItem>
        <Separator />
    </ContextMenu>
    
    <tb:TaskbarIcon x:Key="FreedomNotifyIcon"
                    IconSource="{Binding StatusIconPath}"
                    ContextMenu="{StaticResource FreedomTrayMenu}" MenuActivation="LeftOrRightClick" KeyUp="{Binding OnKeyUp}">
        
        <!-- Self-assign a data context (could also be done programmatically) -->
        <tb:TaskbarIcon.DataContext>
            <local:StatusViewModel />
        </tb:TaskbarIcon.DataContext>
    </tb:TaskbarIcon>
    
</ResourceDictionary>
2 Answers

Try setting OnMouseLeave on the parent MenuItem like this

<ResourceDictionary ... 
    x:Class="ContextMenuHandler">
    <!--...-->
    <MenuItem Header="Options" MouseLeave="OnMouseLeave">
        <!--...-->
    </MenuItem>
    <!--...-->
</ResourceDictionary>

Then also add a class called ContextMenuHandler.cs

    public partial class ContextMenuHandler : ResourceDictionary
    {
        public void OnMouseLeave(object sender, EventArgs e)
        {

        }
    }

If you're bond to use Hardcodet.Wpf.TaskbarIcon, talk with the guy that wrote the TaskBarIcon ;)

If NOT, I suggest to you to use System.Windows.Forms.NotifyIcon and write some C# code, here a question with a good answer, see the answer, accepted , by Jesper Jensen.

Mind that the Icon in tray is owned by Windows and WPF is only a different UI of a Windows exe, that mind that you can use, by C# code, a NotifyIcon or whatever you want from Windows.

NotifyIcon expose methods to handle Click on Balloon, on Icon, Mouse events, etc.

Related