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,

Developer technologies | Windows Presentation Foundation
Developer technologies | XAML
Developer technologies | 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.

Developer technologies | C#
Developer technologies | 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.

0 comments No comments

Answer accepted by question author
Hui Liu-MSFT 48,726 Reputation points Microsoft External Staff
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.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.