4

So I want to implement admin, non-admin (casual) user style priviledges for an app, but it's only on 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?

Joan Venge
  • 315,713
  • 212
  • 479
  • 689

2 Answers2

2

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.

Melllvar
  • 2,056
  • 4
  • 24
  • 47
2

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:

  1. Use a ContentTemplate or a DataTemplate for every piece of UI that you want to make themable
  2. Refer to these templates using {DynamicResource}
  3. Have the resources be pulled from your application resources; your will place them there as ResourceDictionary objects, one per "theme", using ResourceDictionary.MergedDictionaries
  4. 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?

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
  • Thanks Jon, do you mean I should design separate xaml files for each theme? If that's the case, what's the initial, base xaml will be like? Just nothing but an empty xaml code with app.resources like you showed? – Joan Venge Mar 11 '11 at 00:53
  • 1
    @Joan: Pretty much whatever you like. I 'd start with one of the "real" themes as a "sensible default", but it really depends on when/how the user's information is available to your app. – Jon Mar 11 '11 at 00:57
  • Thanks Jon, I think I got it now. – Joan Venge Mar 11 '11 at 01:10
  • I was just trying your method, by following the link you posted, but when I copy my base xaml file basically MainWindow.xaml, VS complains about the same members in each file. I deleted the cs file from the copied one, but I am not sure what the theme xaml files should include or how they would be binded if they have no cs file within them unlike MainWindow.xaml and App.xaml, any ideas? – Joan Venge Mar 15 '11 at 17:37