如何将布尔值绑定到GridViewColumn复选框(有代码但不起作用)?

我试图将bool值绑定到GridViewColumn复选框,但它不起作用。 我甚至试图返回false,但复选框仍然显示为启用。 它只有在我输入“False”进入xaml时才有效。

绑定属性是:

 public bool HasPermissions { get { return this.UserPrivileges == UserPrivileges.FullAccess; } } 

this.UserPrivileges当前值不是UserPrivileges.FullAccess

Xaml代码:

 <Window x:Class="EffectsWindow.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Effects Manager" Width="800" Height="500" DataContext="{Binding RelativeSource={RelativeSource Self}}"                  

编辑:当前房产代码:

 public bool HasPermissions { get { return this.UserPermissions == UserPermissions.FullAccess; } set { this.RaisePropertyChanged ( "HasPermissions" ); } } 

考虑更新属性中的问题:该属性没有后备字段,其getter返回将不同属性与UserPermissions.FullAccess进行比较的结果。 因此永远无法设置。

要关注的是,何时需要通知UI HasPermissions返回的值已更改? 那么,这个价值什么时候可以改变? 当this.UserPermissions的值发生变化时,对吧?

假设this.UserPermissions本身是一个带有setter的属性, 它的setter就是调用RaisePropertyChanged("HasPermissions") 。 这将告诉UI,即使它没有直接绑定到UserPermissions必须重新评估它绑定到的属性。

更新:关于您的评论,如果您希望框的已选中状态指示用户具有权限,则IsChecked确实是您应将HasPermissions绑定到的CheckBox属性。

更新第二个:听起来你想从可视子(ListBox)访问Window的DataContext的属性。 您可以使用RelativeSource绑定来实现它,如下所示:

  

这种有点笨重的符号将在可视化树中找到最接近CheckBox的父元素,它是Window类型,并绑定到其DataContext属性以查找HasPermission。