好的,让我看看我做得对,或者我是在努力做到这一点。
首先。 我有一个整数(0-255)的帐户级别,每个字节设置为启用给定的功能。 我正在努力设置给定字节的可见性 Psudo XML代码
<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}"/>
当用户登录时,会将类ProgramVar.ActiveUser拉出并设置为当前从PHP Soap界面登录的用户(正常工作)
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;
}
现在我也在阅读IValueConverters,使用当前方法是正确的方法,还是应该在XAML&amp;中使用IValueConverts? CS文件?
我正在使用的当前方法正在工作,因为当我尝试使用IValueConvertes时,我没有看到如何让对象返回多个Visibilities(或者如果需要还有其他可用性)
答案 0 :(得分:0)
有些事情你做错了......
Level.AllowX
字段不是属性。 WPF希望属性参与绑定。AllowX
setter应该引发PropertyChangedNotifications(就像在ProgramVar.UserLevel
中一样)。NotifyPropertyChanged("ActiveUser ")
有一个尾随空格!它应该是NotifyPropertyChanged("ActiveUser")
。Visibility
枚举值,因此我认为您的绑定中不需要值转换器。 AllowLevel
字段但不是一个好主意!请检查ItemsControls
和ItemsSource
如何使用List<Visibility>
值!