How to create menu visibile to a certain user WPF

B M-A 361 Reputation points
2022-11-28T11:39:02.853+00:00

Hello,

How is possible in WPF to create menu items visible only to admin or certain user?
I use Mysql and WCF for my project.

Best regards,

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,686 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,377 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
772 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 41,331 Reputation points Microsoft Vendor
    2022-11-29T06:53:39.677+00:00

    You could try to refer to the sample code below according to your needs. And remember to loop through the complete collection, I'm not looping through the collection here.

    iterate
    Xaml:

     <Window.DataContext>  
            <local:ViewModel/>  
        </Window.DataContext>  
        <StackPanel>  
            <CheckBox x:Name="cb" IsChecked="{Binding IsAdmin}"  Content="IsAdmin" Width="100" Height="50"/>  
      
            <Menu DockPanel.Dock="Top" ItemsSource="{Binding MenuItemsView}">  
                <Menu.ItemContainerStyle>  
                    <Style TargetType="{x:Type MenuItem}">  
                        <Setter Property="Command" Value="{Binding Command}" />  
                    </Style>  
                </Menu.ItemContainerStyle>  
                <Menu.ItemTemplate>  
                    <HierarchicalDataTemplate DataType="{x:Type local:MenuItemViewModel}" ItemsSource="{Binding Path=MenuItems}">  
                        <TextBlock Text="{Binding Header}"/>  
                    </HierarchicalDataTemplate>  
                </Menu.ItemTemplate>  
            </Menu>  
        </StackPanel>  
    

    Codebedhind:

    265114-menuitem-user-icollectionview-filter.txt

    The result:

    265115-6.gif

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful