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

Wpf 为什么我的依赖项属性绑定没有更新?,wpf,Wpf,我尝试创建一个多转换器,将当前用户的权限级别与枚举中的权限子集进行比较,以切换某些按钮的可见性。当创建UserControl时,我的转换器在启动时被调用,但是如果我在此之后修改CurrentUser的级别,则不会再次调用转换器 基本结构是: KioskController拥有UserInfo拥有CurrentUser拥有级别。我正在绑定并更新CurrentUser.Level。重要的片段如下: <Image x:Name="Redeem" Height="114" Width="178"

我尝试创建一个多转换器,将当前用户的权限级别与枚举中的权限子集进行比较,以切换某些按钮的可见性。当创建UserControl时,我的转换器在启动时被调用,但是如果我在此之后修改CurrentUser的级别,则不会再次调用转换器

基本结构是:

KioskController拥有UserInfo拥有CurrentUser拥有级别。我正在绑定并更新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 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;

我在控制台窗口中启用了绑定错误,在输出中没有发现任何问题。

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

 MultiBindingExpression be = BindingOperations.GetMultiBindingExpression(Redeem, Image.VisibilityProperty);
            be.UpdateTarget();
BindingExpression be = BindingOperations.GetBindingExpression(Redeem, Image.VisibilityProperty);
            be.UpdateTarget();
或对于单个绑定转换器:

 MultiBindingExpression be = BindingOperations.GetMultiBindingExpression(Redeem, Image.VisibilityProperty);
            be.UpdateTarget();
BindingExpression be = BindingOperations.GetBindingExpression(Redeem, Image.VisibilityProperty);
            be.UpdateTarget();
但是,这有点不友好,因为您的代码隐藏需要知道您在XAML中的绑定,并使用所讨论的转换器为每个对象调用此操作。如果有使用iNotifyPropertyChanged的解决方案,我希望看到它

此链接有助于解决问题: