简体   繁体   中英

WPF ResourceDictionary context menu - menu item visibility property update from other class

I got a ResourceDictionary in my WPF project. The entire project is meant to be running in background. In ResourceDictionary there is a context menu, code is here;

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:Commands="clr-namespace:_365Drive.Office365.NotificationManager"
                    xmlns:tb="http://www.hardcodet.net/taskbar"
                    xmlns:local="clr-namespace:_365Drive.Office365.NotificationManager">

    <LinearGradientBrush x:Key="MenuBackground" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="White" Offset="1" />
        <GradientStop Color="White" Offset="0.259" />
    </LinearGradientBrush>
    <ContextMenu x:Shared="false" x:Key="SysTrayMenu" Name="contextMenu">
        <MenuItem Header="Sign in" Command="{Binding ShowAuthForm}" CommandParameter="{Binding}" />
        <MenuItem Header="Prompt MFA now" Name="MFA"  Command="{Binding ClearMFACacheCommand}" CommandParameter="{Binding}" Visibility="{Binding MFAVisibility}" />
        <MenuItem Header="Sign out" Command="{Binding SignOutApplicationCommand}" CommandParameter="{Binding}" />
        <Separator />
        <MenuItem Header="Update Drive Mappings" Command="{Binding RefreshSettingsCommand}" CommandParameter="{Binding}" />
        <Separator />
        <MenuItem Header="Exit" Command="{Binding ExitApplicationCommand}" />
    </ContextMenu>

</ResourceDictionary>

The code behind of it is as follows (Snippet of code only)

public class NotifyIconViewModel
{
    private Visibility visibility = Visibility.Collapsed;
    public Visibility MFAVisibility
    {
        get
        {
            return ((1) == null ? Visibility.Collapsed : Visibility.Visible);
        }
        set
        {
            visibility = value;
        }
    }
}

There is other code in the code behind but that is irrelevant for this question.

I want to update the above property value from other class in the same project but I am not sure how can I get the instance of that class which is bound with the contextMenu.

And if I do try to set the MenuItem Visibility using code like below, it doesnt work;

public static void DisableMFAMenuitem()
{
    System.Windows.Controls.ContextMenu ctxMenu = (System.Windows.Controls.ContextMenu)System.Windows.Application.Current.FindResource("SysTrayMenu");
    System.Windows.Controls.ItemCollection items = ctxMenu.Items;
    System.Windows.Controls.MenuItem itemtobeRemoved = null;

    foreach (var item in items)
    {
        if (item.GetType() == typeof(System.Windows.Controls.MenuItem))
        {
            // do your work with the item 
            if (((System.Windows.Controls.MenuItem)item).Name == "MFA")
            {
                ((System.Windows.Controls.MenuItem)item).Visibility = Visibility.Collapsed; // OR WHATEVER really
            }
        }
    }
}

I think I am doing some small mistake in understanding, can anyone help here please?

Comment from Grx70 above helped.

I'm not sure I understand what you're trying to achieve here, but from the first glimpse my guess would be that the troublemaker is the x:Shared="False" attribute set on your resource, which means that each FindResource("SysTrayMenu") call will yield a new ContextMenu instance (so you're not modifying any pre-existing instance).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM