【问题标题】:How to bind boolean to GridViewColumn checkboxes (have code but doesn't work)?如何将布尔值绑定到 GridViewColumn 复选框(有代码但不起作用)?
【发布时间】:2011-07-16 22:26:00
【问题描述】:

我正在尝试将布尔值绑定到 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" ); }
}

【问题讨论】:

  • 你确定你已经实现了 INotifyPropertyChanged 吗?
  • 谢谢,我没有实现它。我认为只有在 2 路绑定时才需要 INotifyPropertyChanged?就像值只定义一次一样,你还需要它吗?
  • 对不起还有个意思,这个属性不能设置,只能get。
  • 是的,只要您想将属性中的更改反映到 UI 上,您就需要这样做。试试吧。让我们知道它是否有效。
  • 在这种情况下,您要做的是在 UserPriviliges 属性调用 OnNotifyPropertyChanged("HasPermissions") 的 Setter 中

标签: c# .net data-binding listview gridviewcolumn


【解决方案1】:

想想你更新的属性中的问题:该属性没有支持字段,它的getter返回将不同属性与UserPermissions.FullAccess进行比较的结果。因此它永远不能设置。

重点是,什么时候需要通知UIHasPermissions返回的值发生了变化?那么,什么时候可以改变这个值?当this.UserPermissions 的值发生变化时,对吗?

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

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

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

<CheckBox Margin="0"
          HorizontalAlignment="Center"
          IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.HasPermission}"/>

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

【讨论】:

  • 谢谢,IsChecked 实际上表示效果是否处于活动状态,它使用另一个绑定,但我希望某些人能够修改这些值,而不是每个人。这也是我使用 IsEnabled 的原因。我尝试了您所说的,但仍然启用了复选框,但是 HasPermission 属性返回 False。我还在这个对象的构造函数中设置了 UserPermissions。这有关系吗?
  • 对不起,我犯了一个错误。这很令人困惑。我有一个用于整个窗口的 VM 类,这个名为 EditorVM 的 VM 类具有 ListView 绑定到的可观察集合。但是 HasPermission 属性在此 EditorVM 上,而不是 EditorVM.AllEffects 上。因为属性是全局的,而不是每个 Effect 的实例。您认为我如何将这些复选框绑定到 IsEnabled 属性的 EditorVM?有意义吗?
  • 或者你认为我必须将 ListView 绑定到 EditorVM 并为所有其他绑定使用 AllEffects.PropertyName 表示法,以便我处理 HasPermissions 属性?
  • @Joan Venge 如果我理解正确,听起来您想从可视子项(ListBox)访问 Window 的 DataContext 的属性。请参阅我的更新答案。
  • 谢谢,因为这个问题很独特,所以我专门提出了另一个问题。希望这可以为您提供更多详细信息:stackoverflow.com/questions/5332213/…
猜你喜欢
  • 2015-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
相关资源
最近更新 更多