6

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>
KangJiYoung13
  • 330
  • 4
  • 12
  • The real Win32 HMENU does not do this. – Anders Jun 24 '22 at 00:14
  • You mean there is a problem with the package that we use? – KangJiYoung13 Jun 27 '22 at 14:27
  • The behavior in .NET 6 is different in that the top-level `MenuItem` is not immediately deselected when the mouse leaves but after a slight delay, which allows enough time for the sub-menu to remain open until the mouse enters it. – rucamzu Aug 04 '22 at 12:03

2 Answers2

1

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)
        {

        }
    }
Bron Davies
  • 5,930
  • 3
  • 30
  • 41
1

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.

Davide Dolla
  • 340
  • 4
  • 9