question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MenuBarItem with no Items shows blank Dropdown

See original GitHub issue

Describe the bug It’s sometimes desirable to have a top-level MenuBarItem in a MenuBar that contains no items and is just effectively a ‘button’. This is common in some apps for certain common functions or for a simple ‘Help’ or ‘About’ dialogs.

Steps to reproduce the bug

  1. Copy Keyboard accelerator example from XAML Controls Gallery into XAML Studio
  2. Add a blank MenuBarItem: <muxc:MenuBarItem Title="View"/>
  3. Click on it, note: a blank menu drops down.

Expected behavior No Menu to drop-down, should only act as a button.

Screenshots image

Full XAML Example

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
    mc:Ignorable="d">

<muxc:MenuBar>
  <muxc:MenuBarItem Title="File">
    <MenuFlyoutItem Text="New">
      <MenuFlyoutItem.KeyboardAccelerators>
        <KeyboardAccelerator Modifiers="Control" Key="N"/>
      </MenuFlyoutItem.KeyboardAccelerators>
    </MenuFlyoutItem>
    <MenuFlyoutItem Text="Open...">
      <MenuFlyoutItem.KeyboardAccelerators>
        <KeyboardAccelerator Modifiers="Control" Key="O"/>
      </MenuFlyoutItem.KeyboardAccelerators>
    </MenuFlyoutItem>
    <MenuFlyoutItem Text="Save">
      <MenuFlyoutItem.KeyboardAccelerators>
        <KeyboardAccelerator Modifiers="Control" Key="S"/>
      </MenuFlyoutItem.KeyboardAccelerators>
    </MenuFlyoutItem>
    <MenuFlyoutItem Text="Exit">
      <MenuFlyoutItem.KeyboardAccelerators>
        <KeyboardAccelerator Modifiers="Control" Key="E"/>
      </MenuFlyoutItem.KeyboardAccelerators>
    </MenuFlyoutItem>
  </muxc:MenuBarItem>

  <muxc:MenuBarItem Title="Edit">
    <MenuFlyoutItem Text="Undo">
      <MenuFlyoutItem.KeyboardAccelerators>
        <KeyboardAccelerator Modifiers="Control" Key="Z"/>
      </MenuFlyoutItem.KeyboardAccelerators>
    </MenuFlyoutItem>
    <MenuFlyoutItem Text="Cut">
      <MenuFlyoutItem.KeyboardAccelerators>
        <KeyboardAccelerator Modifiers="Control" Key="X"/>
      </MenuFlyoutItem.KeyboardAccelerators>
    </MenuFlyoutItem>
    <MenuFlyoutItem Text="Copy">
      <MenuFlyoutItem.KeyboardAccelerators>
        <KeyboardAccelerator Modifiers="Control" Key="C"/>
      </MenuFlyoutItem.KeyboardAccelerators>
    </MenuFlyoutItem>
    <MenuFlyoutItem Text="Paste">
      <MenuFlyoutItem.KeyboardAccelerators>
        <KeyboardAccelerator Modifiers="Control" Key="V"/>
      </MenuFlyoutItem.KeyboardAccelerators>
    </MenuFlyoutItem>
  </muxc:MenuBarItem>

  <muxc:MenuBarItem Title="View"/>

  <muxc:MenuBarItem Title="Help">
    <MenuFlyoutItem Text="About">
      <MenuFlyoutItem.KeyboardAccelerators>
        <KeyboardAccelerator Modifiers="Control" Key="I"/>
      </MenuFlyoutItem.KeyboardAccelerators>
    </MenuFlyoutItem>
  </muxc:MenuBarItem>
</muxc:MenuBar>
</Page>

Version Info

NuGet package version: 2.3.191129002

Windows 10 version Saw the problem?
Insider Build (xxxxx)
May 2020 Update (19041) Yes
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT

Additional context Related to gap for #2216

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
Felix-Devcommented, Jun 23, 2020

This would also match the behavior of WPF’s Menu \ MenuItem control:

<Menu Grid.Row="0">
    <MenuItem Header="Options">
        <MenuItem.Items>
            <MenuItem Header="Option 1" />
            <MenuItem Header="Option 2" />
        </MenuItem.Items>
    </MenuItem>
    <MenuItem Header="Help" Click="MenuItem_Click" />
</Menu>

wpf-menuitem-behavior

It’s important to note here that WinUI’s MenuBarItem currently does not have APIs to handle user input like a Click event or a Command property like WPF’s MenuItem does. There is the inherited PointerPressed event from UIElement but that event is not raised for developers to use here.

As such, if it’s agreed that it’s a valid use case to have a MenuBarItem act like a button (when there are no child items) it makes sense to me that we should also add APIs like a Click event or a Command property to the MenuBarItem then.

1reaction
Felix-Devcommented, Jul 17, 2020

I will try to create the proposal this weekend though I’m not sure if every API of it can be added prior to WinUI going full open-source. The Command API for example can probably be best implemented by re-using internal WinUI Command logic (used for other Command properties in WinUI) and, from what I can tell after doign a quick check, that logic is not available in WinUI 2.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the proper way to show blank menu item for ...
setting the MenuItem value to "" and primaryText to "" will make it invisible in the choices. I suggest you do this:
Read more >
Dropdown menu appears blank
Hi. The menu at the top of my homepage has an issue where for the menu items that have a dropdown menu ('Studying'...
Read more >
blank item in dropdown menu - Microsoft Power BI Community
Solved! Go to Solution. Labels: Labels: Need Help · Show and ...
Read more >
How to detect a select menu is blank - Studio
I'm automating filling out a web form that has dynamic Select menus. One of these only populates when valid data is found. If...
Read more >
Solved: Dropdown list value is blank when opening existing...
The only problem I ran into is when I'm going back to edit an existing item. The choices fields did not display/pull the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found