如何绑定布尔值GridViewColumn复选框(有code,但不工作)? [英] How to bind boolean to GridViewColumn checkboxes (have code but doesn't work)?

查看:563
本文介绍了如何绑定布尔值GridViewColumn复选框(有code,但不工作)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个布尔值绑定到一个 GridViewColumn ,但它不工作的复选框。我甚至尝试刚刚返回false,但仍复选框看起来启用。它只是如果我输入假成XAML工作。

的绑定属性是:

 公共BOOL HasPermissions
{
    {返回this.UserPrivileges == UserPrivileges.FullAccess; }
}
 

电流 this.UserPrivileges 的值不是 UserPrivileges.FullAccess

XAML中code:

 <窗​​口x:类=EffectsWindow.MainWindow
        的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
        的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
        标题=效果管理
        宽度=800
        高度=500
        的DataContext ={绑定的RelativeSource = {的RelativeSource自}}
    < D​​ockPanel中VerticalAlignment =拉伸>
        < D​​ockPanel.Resources>

        < ListView的X:名称=EffectsListView
                  的ItemsSource ={结合AllEffects}>

            < ListView.View>
                < GridView控件>

                    < GridViewColumn WIDTH =50标题=覆盖>
                        < GridViewColumn.CellTemplate>
                            <的DataTemplate>
                                <复选框保证金=0
                                          的Horizo​​ntalAlignment =中心
                                          的IsEnabled ={结合HasPermission}/>
                            < / DataTemplate中>
                        < /GridViewColumn.CellTemplate>
                    < / GridViewColumn>

                < / GridView的>
            < /ListView.View>
        < / ListView控件>

    < / DockPanel中>
< /窗>
 

编辑:当前属性code:

 公共BOOL HasPermissions
{
    {返回this.UserPermissions == UserPermissions.FullAccess; }
    集合{this.RaisePropertyChanged(HasPermissions); }
}
 

解决方案

想想在你更新后的属性问题:这家酒店不支持字段,其吸气剂返回与比较不同属性的结果UserPermissions.FullAccess 。因此,它永远的的设置。

的事情关注的是,什么时候该UI需要被通知由 HasPermissions 返回值发生了变化?那么,当的可以的那个值的变化?当 this.UserPermissions 的值发生变化,对不对?

假设 this.UserPermissions 本身就是一个二传手的属性,的二传手是调用的地方 RaisePropertyChanged( HasPermissions)。这将告诉用户界面,即使它不与 UserPermissions 直接,楼市就的确实的绑定到必须重新评估。

更新:关于你的评论,器isChecked 确实是我们应该绑定的复选框属性 HasPermissions ,如果你想在框的选中状态,表明用户有权限。

更新2:这听起来像你想从一个视觉的孩子(列表框)访问该窗口的DataContext的的属性。您可以使用的RelativeSource绑定来实现这一点,像这样:

 <复选框保证金=0
          的Horizo​​ntalAlignment =中心
          的IsEnabled ={绑定的RelativeSource = {的RelativeSource FindAncestor,AncestorType = {X:类型窗口}},路径= DataContext.HasPermission}/>
 

这有点笨重符号将找到最近的父元素在视觉树的类型是窗口的复选框,并绑定到其DataContext属性找到HasPermission。

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" ); }
}

解决方案

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.

这篇关于如何绑定布尔值GridViewColumn复选框(有code,但不工作)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆