0

好的,让我们看看我这样做是否正确,或者我是否正在努力。

第一的。我有一个整数(0-255)的帐户级别,每个字节都设置为启用给定的功能。我正在设置对给定字节伪 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 和 CS 文件中使用 IValueConverts?

我正在使用的 Current 方法正在工作,因为当我尝试使用 IValueConvertes 时,我没有看到如何让对象返回多个 Visibilities(或其他变量,如果需要)

4

1 回答 1

0

你做错了很多事情......

  1. Level.AllowX是字段而不是属性。WPF 期望属性参与绑定。
  2. 将它们转换为属性后,AllowXsetter 应该引发 PropertyChangedNotifications(就像 in 一样ProgramVar.UserLevel)。
  3. NotifyPropertyChanged("ActiveUser ")有一个尾随空格!它应该是NotifyPropertyChanged("ActiveUser")
  4. 由于您已经在设置Visibility枚举值,所以我认为您的绑定中不需要值转换器。
  5. 虽然最终创建 255 个AllowLevel字段是个坏主意!请检查如何ItemsControls使用ItemsSourceList<Visibility>
于 2012-04-20T05:19:13.370 回答