如何将布尔值绑定到 GridViewColumn 复选框(有代码但不起作用)?
我试图将 bool 值绑定到 GridViewColumn 中的复选框,但它不起作用。我什至尝试只返回 false,但复选框看起来仍然处于启用状态。仅当我在 xaml 中输入“False”时它才有效。
绑定属性为:
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}}"
<DockPanel VerticalAlignment="Stretch">
<DockPanel.Resources>
<ListView x:Name="EffectsListView"
ItemsSource="{Binding AllEffects}">
<ListView.View>
<GridView>
<GridViewColumn Width="50" Header="Override">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Margin="0"
HorizontalAlignment="Center"
IsEnabled="{Binding HasPermission}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</DockPanel>
</Window>
编辑:当前属性代码:
public bool HasPermissions
{
get { return this.UserPermissions == UserPermissions.FullAccess; }
set { this.RaisePropertyChanged ( "HasPermissions" ); }
}
I am trying to bind a bool value to checkboxes in a GridViewColumn
, but it doesn't work. I even tried just returning false, but the checkboxes still look enabled. It only works if I type "False" into the xaml.
The binded property is:
public bool HasPermissions
{
get { return this.UserPrivileges == UserPrivileges.FullAccess; }
}
Current value of this.UserPrivileges
is not UserPrivileges.FullAccess
.
Xaml code:
<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}}"
<DockPanel VerticalAlignment="Stretch">
<DockPanel.Resources>
<ListView x:Name="EffectsListView"
ItemsSource="{Binding AllEffects}">
<ListView.View>
<GridView>
<GridViewColumn Width="50" Header="Override">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Margin="0"
HorizontalAlignment="Center"
IsEnabled="{Binding HasPermission}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</DockPanel>
</Window>
EDIT: Current property code:
public bool HasPermissions
{
get { return this.UserPermissions == UserPermissions.FullAccess; }
set { this.RaisePropertyChanged ( "HasPermissions" ); }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑一下更新后的属性中的问题:该属性没有支持字段,其 getter 返回将不同属性与 UserPermissions.FullAccess 进行比较的结果。因此它永远无法被设置。
需要关注的是,什么时候需要通知 UI
HasPermissions
返回的值发生了变化?那么,这个值什么时候可以改变呢?当this.UserPermissions
的值发生变化时,对吧?假设
this.UserPermissions
本身是一个带有 setter 的属性,其 setter 是调用RaisePropertyChanged("HasPermissions")
的地方。这将告诉 UI,即使它不直接绑定到 UserPermissions,也必须重新评估它绑定的属性。更新:关于您的评论,
IsChecked
确实是您应该将HasPermissions
绑定到的CheckBox属性,如果您希望框的选中状态表明用户有权限。更新第二个: 听起来您想从可视子项(ListBox)访问窗口的 DataContext 的属性。您可以使用RelativeSource 绑定来实现这一点,如下所示:
这种有点笨拙的表示法将在可视化树中查找距离CheckBox 最近的Window 类型父元素,并绑定到其DataContext 属性以查找HasPermission。
Think about the problem in your updated property: that property has no backing field, its getter returns the result of comparing a different property with
UserPermissions.FullAccess
. Therefore it can never be set.The thing to focus on is, when does the UI need to be notified that the value returned by
HasPermissions
has changed? Well, when can that value change? When the value ofthis.UserPermissions
changes, right?Assuming
this.UserPermissions
is itself a property with a setter, its setter is the place to callRaisePropertyChanged("HasPermissions")
. That will tell the UI that, even if it doesn't bind toUserPermissions
directly, the property it does bind to must be re-evaluated.Update: Regarding your comment,
IsChecked
is indeed the CheckBox property you should bindHasPermissions
to if you want the box's checked state to indicate that the user has permission.Update the 2nd: It sounds like you want to access a property of the Window's DataContext from a visual child (the ListBox). You can use a RelativeSource binding to achieve that, like so:
That somewhat clunky notation will find the nearest parent element to the CheckBox in the visual tree that is of type Window, and bind to its DataContext property to find HasPermission.