我试图创建一个 MultiConverter,它将 CurrentUser 的权限级别与枚举中的权限子集进行比较,以切换某些按钮的可见性。我的转换器在创建 UserControl 时在启动时被调用,但如果我在此之后修改 CurrentUser 的级别,则不会再次调用转换器。
基本结构是:
KioskController 拥有 UserInfo 拥有 CurrentUser 拥有 Level。我正在绑定并更新 CurrentUser.Level。重要片段如下:
<Image x:Name="Redeem" Height="114" Width="178" Source="Graphics\MAINMENU_Redeem.png" Margin="128,260,718,394">
<Image.Visibility>
<MultiBinding Converter="{StaticResource theIntPermissionToVisibilityConverter}">
<Binding Path="_UserInfo.CurrentUser.Level"/>
<Binding Source="{x:Static local:UserInfo+UserLevel.Cashier}"/>
<Binding Source="{x:Static local:UserInfo+UserLevel.Manager}"/>
<Binding Source="{x:Static local:UserInfo+UserLevel.Tech}"/>
</MultiBinding>
</Image.Visibility>
</Image>
信息亭控制器:
public sealed class KC : DependencyObject
{
/// <summary>
/// Singleton interface to the Kiosk Controller
/// </summary>
///
#region _UserInfoDependencyProperty
public UserInfo _UserInfo
{
get { return (UserInfo)this.GetValue(_UserInfoProperty); }
set { this.SetValue(_UserInfoProperty, value); }
}
public static readonly DependencyProperty _UserInfoProperty = DependencyProperty.Register(
"_UserInfo", typeof(UserInfo), typeof(KC), new PropertyMetadata(null));
#endregion
用户信息类:
public class UserInfo : DependencyObject
{
#region CurrentUserProperty
public User CurrentUser
{
get { return (User)this.GetValue(CurrentUserProperty); }
set { this.SetValue(CurrentUserProperty, value); }
}
public static readonly DependencyProperty CurrentUserProperty = DependencyProperty.Register(
"CurrentUser", typeof(User), typeof(UserInfo), new PropertyMetadata(null));
#endregion
最后是用户类:
public class User : DependencyObject
{
#region UserLevelProperty
public UserInfo.UserLevel Level
{
get { return (UserInfo.UserLevel)this.GetValue(LevelProperty); }
set { this.SetValue(LevelProperty, value); }
}
public static readonly DependencyProperty LevelProperty = DependencyProperty.Register(
"UserLevel", typeof(UserInfo.UserLevel), typeof(User), new PropertyMetadata(UserInfo.UserLevel.Invalid));
#endregion
我正在将用户控件的 DataContext 设置为我的 KioskController,这似乎正在工作。我已经在文本块中测试了一个简单的字符串绑定,它显示正常。
最后,更新 CurrentUser 的调用会触发 Setter,但不会导致再次调用转换器:
CurrentUser.Level = theUser.Level;
我在控制台窗口中启用了绑定错误,并且在输出中看不到任何问题。