tags:

views:

374

answers:

1

I have WPF window that uses a dockpanel and the menu control. I have the code to create the menu options based on a user ID.

Within this window, I have a frame that contains a WPF page. I carry out all the authentication on the page and then have a user ID for the window to use. However, I cannot get the parent window to "refresh" and create the menu bar with the new ID information. When the window loads, I do not run through the commands to display the menu bar. I have tried putting that in its own, public, function and calling it from the page but that does not seem to work.

There must be a window method that I'm missing that can make the menu bar display based on the call from the page.

A: 

It sounds like you're still thinking in procedurally, in WinForms style. What you describe would be necessary in WinForms, but in WPF it is usually much easier: just use data binding. As long as your menu items are generated from a "UserID" dependency property (or enabled/disabled based on it), then all you need to do is set the UserID DependencyProperty and the UI will update itself with no additional code.

Here is how to get the UserID into a DependencyProperty of the Window or a context object:

  • In your Window or a context object create a "UserID" DP
  • Make your Window or your a context object the DataContext of the page
  • At the end of the authentication code, set DataContext.UserID (or alternatively create a UserID property on the page, and have the Window bind to it with a two way binding)

Once you have the UserID in a DependencyProperty, there are many ways to update the menu items automatically whenever it changes:

  • In each menu item, bind its visibility to the UserID DP on the window using an appropriate converter (using the converter parameter to distinguish between items), -OR-
  • Use a converter for setting ItemsSource so you filter your items, -OR-
  • Create a PropertyChangedCallback that sets a filter on the CollectionView you use for menu items, -OR-
  • Some other technique (there are many other good ways to do this)

For typical situations we're talking about less than 10 lines of C# here, not counting the DependencyProperty declarations.

Ray Burns