0

I am creating a desktop application using WPF c# in Visual Studio 2010 express. I have created a menu bar which I would like to display certain elements depending on user access level. I can set the menu visibility to Hidden as default but finding it difficult to set the visitbility to Visible thereafter once a successful sign in has been made. Below is the sample of the 'xaml' code and the c# code.

'XAML code'

<Menu Name="MenuBar" VerticalAlignment="Top" Width="Auto" Margin="0,0,0,389">
    <MenuItem Header="_Maintenance" Margin="2,0,0,2" Width="Auto"
              Visibility="hidden" Click="MenuItem_Click">
        <MenuItem Header="Customer Maintenance"/>
        <MenuItem Header="Staff Maintenance"/>
        <MenuItem Header="User Maintenance"/>
        <MenuItem Header="Item Maintenance"/>
        <MenuItem Header="Standing Maintenance"/>
    </MenuItem>
</Menu>

My attempt on the 'C# code'

public MainWindow()
{
    InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    MenuBar.Visibility="Visible";

    //Load and display sign in screen
    App1 app = new App1();
    app.LoadSignIn();
}

Errors I'm facing are:

Cannot implicitly convert type 'string' to 'System.Windows.Visibility'

  • Are you using the MVVM design pattern in your application? – Colin Jun 04 '13 at 03:22
  • Hi Colin, no i'm not, infact not heard of it. Is this something i should look into? Is there a good place where I can look for more info on this or should i just google? – user2449833 Jun 04 '13 at 11:45
  • It is a design pattern used widely in WPF. Here is a [Link](http://www.codeproject.com/Articles/100175/Model-View-ViewModel-MVVM-Explained) which introduces it. Your question would be straightforward if you use it. Hoping that it is helpful. – Colin Jun 04 '13 at 12:31

2 Answers2

0

You'll want to bind to a Style Trigger to get this behavior, like so (excerpted from one of the links below, modified to match your supplied xaml):

<Window.Resources>
  <Style x:Key="VisibleWhenUserAllowedAccess" TargetType="MenuItem">
      <Style.Triggers>
          <DataTrigger Binding="{Binding IsUserAllowedAccess}" Value="False">
              <Setter Property="Visibility" Value="Hidden"/>
          </DataTrigger>
    </Style.Triggers>
  </Style>

<Menu Name="MenuBar" VerticalAlignment="Top" Width="Auto" Margin="0,0,0,389">
    <MenuItem Header="_Maintenance" Margin="2,0,0,2" Width="Auto"
          Click="MenuItem_Click" 
          Style="{StaticResource VisibleWhenFileIsOpen}">
        <MenuItem Header="Customer Maintenance"/>
        <MenuItem Header="Staff Maintenance"/>
        <MenuItem Header="User Maintenance"/>
        <MenuItem Header="Item Maintenance"/>
        <MenuItem Header="Standing Maintenance"/>
    </MenuItem>
</Menu>

More reading on changing visibility of menus in WPF in these two links:

WPF UserControl Context Menu Visibility Binding

http://www.codeproject.com/Articles/37848/WPF-Data-Bound-Menus

Community
  • 1
  • 1
Gjeltema
  • 4,122
  • 2
  • 24
  • 31
0

Your code

MenuBar.Visibility="Visible";

is not correct.

Visibility is an enum type instead of string. You can set it like this:

MenuBar.Visibility =Visibility.Visible.
Colin
  • 551
  • 2
  • 10
  • This won't work - I tried it out just to confirm when I looked at this last night. – Gjeltema Jun 04 '13 at 12:40
  • You are correct that it addresses his exact stated question, but it won't resolve his actual issue. If he assigns the value correctly from the enum, then he will be right back asking "how do I get it to work since assigning it the enum doesn't change the visibility?". I tend to agree with minimal answering of the question in most cases, but in this case it seems that the next step of answering should be taken. – Gjeltema Jun 05 '13 at 03:30