0

我试图创建一个 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;

我在控制台窗口中启用了绑定错误,并且在输出中看不到任何问题。

4

1 回答 1

0

据我了解,更改 DependencyProperty 会导致绑定到它的元素更新,但不会导致转换器重新评估。这似乎通常被认为是疏忽或错误。解决方案是强制重新评估转换器:

 MultiBindingExpression be = BindingOperations.GetMultiBindingExpression(Redeem, Image.VisibilityProperty);
            be.UpdateTarget();

或对于单个绑定转换器:

BindingExpression be = BindingOperations.GetBindingExpression(Redeem, Image.VisibilityProperty);
            be.UpdateTarget();

但是,这有点不友好,因为您的代码隐藏需要了解您在 XAML 中拥有的绑定,并使用相关转换器为每个对象调用此操作。如果有使用 iNotifyPropertyChanged 的​​解决方案,我想看看。

此链接帮助解决了问题: DependencyObject.InvalidateProperty not working

于 2013-07-26T16:47:09.400 回答