为什么我的依赖属性绑定没有更新?

时间:2013-07-26 14:17:25

标签: wpf

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

KioskController:

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

UserInfo类:

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

我正在将usercontrol的DataContext设置为我的KioskController,这似乎正在运行。我在一个文本块中测试了一个简单的字符串绑定,它显示正常。

最后,更新CurrentUser的调用会触发Setter,但永远不会再次调用转换器:

CurrentUser.Level = theUser.Level;

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

1 个答案:

答案 0 :(得分: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