How to hide, enable, change WPF GUIs based on different types of users?
So I want to implement admin, non-admin (casual)
user style priviledges for an app, but it's only o开发者_如何学编程n the GUI level.
So for instance, to disable a few GridViewColumns
, hide some buttons, etc.
What's the proper way to do this in WPF?
I plan to implement an enum for user types. But after that I am not sure how to enable/disable GridViewColumns
, hide/show buttons.
Any ideas?
It depends on if you want to be able to change themes (because in essence this is what you want to do) on the fly (I 'm assuming you do).
I haven't done this myself, but I think it should go like this:
- Use a
ContentTemplate
or aDataTemplate
for every piece of UI that you want to make themable - Refer to these templates using
{DynamicResource}
- Have the resources be pulled from your application resources; your will place them there as
ResourceDictionary
objects, one per "theme", usingResourceDictionary.MergedDictionaries
- Whenever you want to change "theme", programmatically remove all current merged dictionaries and add the one corresponding to the desired "theme"
To illustrate, your Application
would use a default dictionary like this:
<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 'm describing: Can WPF themes be used to include multiple skins for an application that can be changed at runtime?
One implementation would be to create an object that contains the permissions, bind it to the root data context, and use property triggers to enable/disable various parts of the UI. Additional information can be found here.
精彩评论