0

I want create a user permission validation to my system, I have created a enum class Permission contained all type of permissions, example: CanInsert, CanEdit, CanAccess, CanDelete, etc...

And in my class User has a property List contained all permission this user has

I created a converter PermissionToVisibleHiddenConverter for use in Visibility of controls

public class PermissionToVisibleHiddenConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        User loger = (User)value;

        /* Before edit
        if (loger.Permissao.Contains(Permission.Principal_AbrirLoja))
            return Visibility.Collapsed;
        else
            return Visibility.Collapsed; */

        if (loger.Permissao.Contains(Permission.Principal_AbrirLoja))
            return Visibility.Visible;
        else
            return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

But it is not working when I try binding with this code

<!--<MenuItem Header="Abrir loja" Visibility="{Binding Loger, Converter={StaticResource BoolToVisibleHiddenConverter}, ElementName=window, TargetNullValue=Collapsed, Mode=OneWay}"/>-->
<MenuItem Header="Abrir loja" Visibility="{Binding Loger, Converter={StaticResource PermissionToVisibleHiddenConverter }, ElementName=window, TargetNullValue=Collapsed, Mode=OneWay}"/>

Where window is this MenuItem parent similar this, but Loger is not null

public partial class MainWindow : Window
{
    User Loger { get; set; }

    public MainWindow()
    {
        InitializeComponent();
    }
}

This code will not throw any exception or error, just do nothing

Where is problem?

Lai32290
  • 8,062
  • 19
  • 65
  • 99
  • 2
    In condition `loger.Permissao.Contains (Permission.Principal_AbrirLoja)` you in both cases the return `Visibility.Collapsed`. So conceived or is it a typo? – Anatoliy Nikolaev Jan 03 '14 at 19:45
  • yes, I had put it for test, but my MenuItem is always Visible – Lai32290 Jan 03 '14 at 20:34
  • In `MenuItem` you use `BoolToVisibleHiddenConverter`, but in question was indicated `PermissionToVisibleHiddenConverter`... possible for the test. Anyway, try set `Mode="TwoWay"` and remove `ElementName`, (you can also remove `TargetNullValue`) maybe help. – Anatoliy Nikolaev Jan 03 '14 at 20:40
  • I tried it now, same thing, nothing error nothing effect – Lai32290 Jan 03 '14 at 20:49

1 Answers1

1

1) Your property is not public 2) you need to notify

public partial class Window1 : Window,INotifyPropertyChanged
{
    public Window1()
    {
        InitializeComponent();
    }

    User loger;

    public User Loger 
    { 
        get{return  loger;}
        set { loger= value; OnPropertyChanged("Loger "); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
yo chauhan
  • 12,079
  • 4
  • 39
  • 58