none
Hiding UI elements according user role

    Question

  • I created a custom RoleToVisibilityClass as follows:
    public class RoleToVisibilityConverter : IValueConverter
    {
    public object
    Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { Web.User user = value as Web.User; if (user != null) { return user.IsInRole((string)parameter) ? Visibility.Visible : Visibility.Collapsed; } else { return null; } }

    Changes in my App.xaml

    <Application xmlns:app_helpers="clr-namespace:ReportingTool_SL4_v2.Helpers">
    <ResourceDictionary>
        <app_helpers:RoleToVisibilityConverter x:Key="RoleToVisibilityConverter" />
    </ResourceDictionary>

    I try to use this converter to hide some UI elements (menu links) for unauthorized users in my MainPage.xaml

    <HyperlinkButton x:Name="SuperLink" Style="{StaticResource LinkStyle}" 
           NavigateUri="/Supervisor" TargetName="ContentFrame"
           Visibility="{Binding Path=WebContext.Current.User, Mode=OneWay, Converter={StaticResource RoleToVisibilityConverter}, ConverterParameter=Supervisor}"/> 

    However, my  link is still visible for all users. Even if I change my converter to

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {     
       return Visibility.Collapsed;
    }

    link in menu is still visible. This must mean that I have an error in XAML file, but where?

    Tuesday, June 22, 2010 2:49 PM

Answers

  • In your hyperlink set the DataContext to... 

    DataContext="{StaticResource WebContext}"

    and then update the path to...

    Visibility="{Binding Path=Current.User

    You will also be able to debug now. 

     

    Monday, June 28, 2010 11:36 AM

All replies

  • Did you debug the ValueConverter to see what it returns?
    Tuesday, June 22, 2010 3:05 PM
  • Sorry, misread your original message... Ignore.
    Tuesday, June 22, 2010 3:22 PM
  • Did you debug the ValueConverter to see what it returns?

     

    I tried, but when I add a breakpoint to my ValueConverter, VS says "The breakpoint will not currently be hit. No symbols have been loaded for this document". So I changed my ValueConverter to always return a Visibility.Collapsed value. My link is still visible, so it seems, that my ValueConverter class is never called.

    Tuesday, June 22, 2010 3:22 PM
  • Hi,

    Is the converter in a referenced assembly? If this is the case, please make sure the assembly is properly loaded, check this blog:

    http://geekswithblogs.net/dbutscher/archive/2007/06/26/113472.aspx

    Thursday, June 24, 2010 2:02 AM
  • Debug/Breakpoint Problem - I couldn't get it to work.

    However, my Converter seems to work correctly, I'm using it from code-behind

    private void HideUIElements()
    {
        Helpers.RoleToVisibilityConverter role = new Helpers.RoleToVisibilityConverter();
        object v = role.Convert(WebContext.Current.User, null, "Supervisor", System.Threading.Thread.CurrentThread.CurrentCulture);
        this.Divider2.Visibility = (System.Windows.Visibility)v;
        this.SuperLink.Visibility = (System.Windows.Visibility)v;
    }

    something must be wrong with my XAML code, but what?

     

    <HyperlinkButton x:Name="SuperLink" Style="{StaticResource LinkStyle}" 
                       NavigateUri="/Supervisor" TargetName="ContentFrame" Content="{Binding Path=ApplicationStrings.SupervisorPageTitle, Source={StaticResource ResourceWrapper}}"
                       Visibility="{Binding Path=WebContext.Current.User, Mode=OneWay, Converter={StaticResource RoleToVisibilityConverter}, ConverterParameter=Supervisor}"/>
                            
                         
      
    Thursday, June 24, 2010 3:13 PM
  • Converter={StaticResource RoleToVisibilityConverter},

    Where is that resource defined? If it's in the app, I'd move it to the page and see if that makes any difference.

    Thursday, June 24, 2010 3:21 PM
  • Where is that resource defined? If it's in the app, I'd move it to the page and see if that makes any difference.

     

    No difference. I tried both, in app.xaml:

     

    <Application   
      x:Class="ReportingTool_SL4_v2.App"
      xmlns:app_helpers="clr-namespace:ReportingTool_SL4_v2.Helpers"
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary>
                        <app_helpers:RoleToVisibilityConverter x:Key="RoleToVisibilityConverter" />
                    </ResourceDictionary>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>                
        </Application.Resources>
    </Application>
    and in MainPage.xaml:
    <UserControl 
      xmlns:app_helpers="clr-namespace:ReportingTool_SL4_v2.Helpers"
        <UserControl.Resources>
            <ResourceDictionary>
                <app_helpers:RoleToVisibilityConverter x:Key="RoleToVisibilityConverter"/>
            </ResourceDictionary>
        </UserControl.Resources>
     
    Monday, June 28, 2010 11:18 AM
  • In your hyperlink set the DataContext to... 

    DataContext="{StaticResource WebContext}"

    and then update the path to...

    Visibility="{Binding Path=Current.User

    You will also be able to debug now. 

     

    Monday, June 28, 2010 11:36 AM
  •  It works! Thanks!

    Monday, June 28, 2010 12:43 PM