I have found a solution for my problem, but I had to replace PopupMenu
with FlyoutMenu
. Instead of using Commands
from PopupMenu
I just attached click event handlers to items.
public static class FlyoutHelper
{
public static readonly DependencyProperty IsHiddenProperty =
DependencyProperty.RegisterAttached("IsHidden", typeof(bool), typeof(FlyoutHelper), new PropertyMetadata(false, OnIsHiddenPropertyChanged));
public static void SetIsHidden(DependencyObject d, bool value)
{
d.SetValue(IsHiddenProperty, value);
}
public static bool GetIsHidden(DependencyObject d)
{
return (bool)d.GetValue(IsHiddenProperty);
}
private static void OnIsHiddenPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var flyout = d as FlyoutBase;
bool isHidden = (bool)e.NewValue;
if (flyout != null && isHidden)
{
flyout.Hide();
}
}
}
Usage:
<MenuFlyout helpers:FlyoutHelper.IsHidden="{Binding IsFlyoutHidden}">
<MenuFlyoutItem Click="Item1Clicked" />
<MenuFlyoutItem Click="Item2Clicked" />
</MenuFlyout>
To hide a flyout you only need to switch IsFlyoutHidden
in view model to true
.