0

Ok lets see if I'm doing this right, or am I doing it the hard way.

First. I have a account level that's a integer (0-255) each Byte is set to enable given functions. I'm working on setting up the Visibility to a Given Byte Psudo XML Code

<Button Grid.Column="0" Grid.Row="0" Name="Function" Content="Function 1" Visibility="{Binding UserLevel.Allow1 , UpdateSourceTrigger=PropertyChanged}"/>
<Button Grid.Column="0" Grid.Row="1" Name="Function" Content="Function 2" Visibility="{Binding UserLevel.Allow2 , UpdateSourceTrigger=PropertyChanged}"/>
<Button Grid.Column="0" Grid.Row="2" Name="Function" Content="Function 3" Visibility="{Binding UserLevel.Allow3 , UpdateSourceTrigger=PropertyChanged}"/>
<Button Grid.Column="0" Grid.Row="3" Name="Function" Content="Function 4" Visibility="{Binding UserLevel.Allow4 , UpdateSourceTrigger=PropertyChanged}"/>
<Button Grid.Column="0" Grid.Row="4" Name="Function" Content="Function 5" Visibility="{Binding UserLevel.Allow5 , UpdateSourceTrigger=PropertyChanged}"/>
<Button Grid.Column="0" Grid.Row="5" Name="Function" Content="Function 6" Visibility="{Binding UserLevel.Allow6 , UpdateSourceTrigger=PropertyChanged}"/>
<Button Grid.Column="0" Grid.Row="6" Name="Function" Content="Function 7" Visibility="{Binding UserLevel.Allow7 , UpdateSourceTrigger=PropertyChanged}"/>
<Button Grid.Column="0" Grid.Row="7" Name="Function" Content="Function 8" Visibility="{Binding UserLevel.Allow8 , UpdateSourceTrigger=PropertyChanged}"/>

When a User signs in , the class ProgramVar.ActiveUser gets pulled and set to the current signed in User from a PHP Soap Interface (working)

class ProgramVar : INotifyPropertyChanged
{
    private User _ActiveUser;
    private Level _UserLevel;
    public User ActiveUser { get { return this._ActiveUser; } set { _ActiveUser= value; UpdateLevel(); NotifyPropertyChanged("ActiveUser "); } }
    public Level UserLevel { get { return this._UserLevel; } set { _UserLevel= value; NotifyPropertyChanged("UserLevel"); } }
    private void UpdateLevel()
    {
        UserLevel.Allow1 = Visibility.Collapsed; if(ActiveUser.AccountLevel & 1) UserLevel.Allow1 = Visibility.Visible;
        UserLevel.Allow2 = Visibility.Collapsed; if(ActiveUser.AccountLevel & 2) UserLevel.Allow2 = Visibility.Visible;
        UserLevel.Allow3 = Visibility.Collapsed; if(ActiveUser.AccountLevel & 4) UserLevel.Allow3 = Visibility.Visible;
        UserLevel.Allow4 = Visibility.Collapsed; if(ActiveUser.AccountLevel & 8) UserLevel.Allow4 = Visibility.Visible;
        UserLevel.Allow5 = Visibility.Collapsed; if(ActiveUser.AccountLevel & 16) UserLevel.Allow5 = Visibility.Visible;
        UserLevel.Allow6 = Visibility.Collapsed; if(ActiveUser.AccountLevel & 32) UserLevel.Allow6 = Visibility.Visible;
        UserLevel.Allow7 = Visibility.Collapsed; if(ActiveUser.AccountLevel & 64) UserLevel.Allow7 = Visibility.Visible;
        UserLevel.Allow8 = Visibility.Collapsed; if(ActiveUser.AccountLevel & 128) UserLevel.Allow8 = Visibility.Visible;
        NotifyPropertyChanged("UserLevel");
    }
}
class User
{
  string UserName;
  string FirstName;
  int AccountLevel;
}
class Level
{
  Visibility Allow1 = Visibility.Visible;
  Visibility Allow2 = Visibility.Visible;
  Visibility Allow3 = Visibility.Visible;
  Visibility Allow4 = Visibility.Visible;
  Visibility Allow5 = Visibility.Visible;
  Visibility Allow6 = Visibility.Visible;
  Visibility Allow7 = Visibility.Visible;
  Visibility Allow8 = Visibility.Visible;
}

Now I'm also reading about IValueConverters, would using the current method be the proper method, or should I be using IValueConverts in the XAML & the CS Files?

The Current method I'm using is working, as when I tried to use the IValueConvertes, I didn't see how to get a object to return multiple Visibilities (or other varables if needed)

PGP_Protector
  • 218
  • 3
  • 12
  • If your implementation is working, I guess using IValueConverters would not be a great idea as it would make things complicated for you. You can stick to this implementation. I would have rather user a List or ObservableCollection of Visibility as type and used it like visibilityCollection[index]. IValueConverters are usually used when the visibility of a control or section of code depends on the value of another control like a radio button or check box or may be some other simple logic. – Shakti Prakash Singh Apr 20 '12 at 06:47
  • It would be hard for you to implement this with your logic as ActiveUSer AccountLevel is also taken into picture and you would need to write the converter in your ViewModel rather than a seperate converter class as your ViewModel logic goes in the Visibility Conversion. – Shakti Prakash Singh Apr 20 '12 at 06:47

1 Answers1

0

There are set of things you are doing wrong...

  1. Your Level.AllowX are fields not properties. WPF expects properties to participate in bindings.
  2. After you have converted them to properties, the AllowX setters should raise PropertyChangedNotifications (just like in ProgramVar.UserLevel).
  3. Your NotifyPropertyChanged("ActiveUser ") has a trailing space! it should be NotifyPropertyChanged("ActiveUser").
  4. As you are already setting Visibility enum values so I dont think there is any need for value converters in your bindings.
  5. Although eventually creating 255 AllowLevel fields is bad idea! Please check how ItemsControls and ItemsSource work with List<Visibility> values!
WPF-it
  • 19,625
  • 8
  • 55
  • 71