How to hide, enable, change WPF GUIs based on different types of users?

So, I want to implement admin, non-admin (casual) style attributes for the application, but this is only at the GUI level.

So, for example, to disable several GridViewColumns , hide some buttons, etc.

What is the correct way to do this in WPF?

I plan to implement an enumeration for custom types. But after that I am not sure how to enable / disable GridViewColumns , hide / show buttons.

Any ideas?

+4
source share
2 answers

It depends on whether you want to change topics (because in essence this is what you want to do) on the fly (I assume that you do it).

I did not do this myself, but I think it should look like this:

  • Use a ContentTemplate or DataTemplate for each fragment of the user interface that you want to make available
  • {DynamicResource} these patterns with {DynamicResource}
  • Extract resources from application resources; you will place them there as ResourceDictionary objects, one per theme using ResourceDictionary.MergedDictionaries
  • Whenever you want to change the "theme", programmatically delete all current merged dictionaries and add the appropriate one corresponding to the desired theme

To illustrate, your Application will use the default dictionary:

 <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="themes\default.theme.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> 

And you would add / remove dictionaries with something like

 ResourceDictionary theme = (ResourceDictionary)Application.LoadComponent(themeUri); Resources.MergedDictionaries.Add(theme); 

Update: I searched a bit and found a more complete example of what I am describing: Can I use WPF themes to include multiple skins for an application that can be changed at runtime?

+2
source

One implementation will be to create an object that contains permissions, bind it to the root data context, and use property triggers to enable / disable various parts of the user interface. Further information can be found here .

+2
source

Source: https://habr.com/ru/post/1343262/


All Articles