Question

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?

Was it helpful?

Solution

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));
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top