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

发布于 2024-10-23 22:41:07 字数 1854 浏览 4 评论 0原文

我试图将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蔚蓝源自深海 2024-10-30 22:41:07

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

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

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

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

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

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

这种有点笨拙的表示法将在可视化树中查找距离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 of this.UserPermissions changes, right?

Assuming this.UserPermissions is itself a property with a setter, its setter is the place to call RaisePropertyChanged("HasPermissions"). That will tell the UI that, even if it doesn't bind to UserPermissions directly, the property it does bind to must be re-evaluated.

Update: Regarding your comment, IsChecked is indeed the CheckBox property you should bind HasPermissions 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:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文