简体   繁体   English

WPF MVVM ListBox.ItemTemplate CheckBox IsChecked绑定

[英]WPF MVVM ListBox.ItemTemplate CheckBox IsChecked binding

The scenario I'm working with is editing Roles and Permissions. 我正在使用的方案是编辑角色和权限。 In a list box I want to list all defined Permissions and check the Permissions the selected Role has been assigned. 在列表框中,我要列出所有定义的权限,然后检查已为选定角色分配的权限。 The Role selection occurs in a separate list. 角色选择出现在单独的列表中。

I have a simple view that contains a list box that displays all permissions: 我有一个简单的视图,其中包含一个显示所有权限的列表框:

<ListBox             
    ...
    ItemsSource="{Binding AllPermissions}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding DisplayName}" 
                      IsChecked="???"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

The ItemsSource is one set of Permissions and the selected role's Permissions is a different set. ItemsSource是一组权限,而所选角色的Permissions是另一组。 How would I bind the IsChecked value to the intersection of the sets (ie, if the Permission in the ListBox is also in the selected role's Permissions then the box should be checked)? 如何将IsChecked值绑定到集合的交集(即,如果ListBox中的权限也位于所选角色的Permissions中,则应选中该框)?

I'm guessing your Item source of AllPermissions is a collection of Permission objects. 我猜你的AllPermissions的Item来源是Permission对象的集合。 So just make sure that in addition to the DisplayName that it also has something on there that determines whether the role has the permission: 因此,只需确保除了DisplayName之外,它还具有确定该角色是否具有权限的内容:

public class Permission : ViewModelBase
{
    private string displayName;
    private bool roleHasPermission;

    public string DisplayName
    {
        get
        {
            return this.displayName;
        }
        set
        {
            this.displayName = value;
            this.RaisePropertyChanged(() => this.DisplayName);
        }
    }

    public bool RoleHasPermission
    {
        get
        {
            return this.roleHasPermission;
        }
        set
        {
            this.roleHasPermission = value;
            this.RaisePropertyChanged(() => this.RoleHasPermission);
        }
    }
}

so then Bind IsChecked to RoleHasPermission. 所以将IsChecked绑定到RoleHasPermission。

Now I'm guessing at the moment that you are loading the available permissions from somewhere and they are currently ignorant of if the role has thepermission, so when you are loading the AllPermissions, calculate whether the rolehas the permission. 现在,我猜测您正在从某个位置加载可用权限,而它们当前不知道角色是否具有权限,因此在加载AllPermissions时,请计算角色是否具有权限。

I have assumed you have inherited from a base class that has a RaisePropertyChanged event on it to notify the view when the value has been updated. 我假设您已从具有RaisePropertyChanged事件的基类继承,以在值已更新时通知视图。 (such as provided for you if you use mvvm light or other frameworks, or you can write your own) Also if you want to be able to edit the permission by checking/unchecking the check box, then remember to set the binding Mode=TwoWay ie: (例如,如果您使用mvvm light或其他框架,则提供给您,或者您可以编写自己的框架)此外,如果您希望能够通过选中/取消选中复选框来编辑权限,那么请记住设置绑定模式= TwoWay即:

<ListBox             
...
ItemsSource="{Binding AllPermissions}">
<ListBox.ItemTemplate>
    <DataTemplate>
        <CheckBox Content="{Binding DisplayName}" 
                  IsChecked="{Binding RoleHasPermission, Mode=TwoWay}"/>
    </DataTemplate>
</ListBox.ItemTemplate>

I would try using the Converter property of the binding for IsChecked. 我会尝试使用IsChecked绑定的Converter属性。 The ConverterParameter for the binding would be set to the list of permissions for the selected role. 绑定的ConverterParameter将设置为所选角色的权限列表。

I don't know how familiar you are with ValueConverters, but I could write some code to illustrate if that would help. 我不知道您对ValueConverters有多熟悉,但是我可以编写一些代码来说明是否有帮助。 ValueConverter examples are easy to find on the internet. 在互联网上很容易找到ValueConverter示例。 The first time I ever implemented a ValueConverter, I followed this example from David Anson's blog . 第一次实现ValueConverter时,我遵循了David Anson博客中的示例

I would write a ViewModel for your Role that has a collection of Permissions. 我会为您的Role编写一个ViewModel,该模型具有Permissions的集合。

public class PermissionViewModel : ViewModelBase
{
    public string Name { get; set; }
    public bool HasPermission { get; set; }
}

public class RoleViewModel : ViewModelBase
{
    public string Name { get; set; }
    public ObservableCollection<PermissionViewModel> PermissionViewModels { get; set; }
}

<ListBox             
...
ItemsSource="{Binding SelectedRoleViewModel.PermissionViewModels}">
<ListBox.ItemTemplate>
    <DataTemplate>
        <CheckBox Content="{Binding Name}" 
                  IsChecked="{Binding HasPermission, Mode=TwoWay}"/>
    </DataTemplate>
</ListBox.ItemTemplate>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM