WPF MVVM ListBox.ItemTemplate CheckBox IsChecked 绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15953897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF MVVM ListBox.ItemTemplate CheckBox IsChecked binding
提问by wilkssh
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. How would I bind the IsChecked value to the intersection of the sets (i.e., if the Permission in the ListBox is also in the selected role's Permissions then the box should be checked)?
ItemsSource 是一组权限,而所选角色的权限是另一组。我将如何将 IsChecked 值绑定到集合的交集(即,如果 ListBox 中的权限也在所选角色的权限中,则应选中该框)?
采纳答案by SteveL
I'm guessing your Item source of AllPermissions is a collection of Permission objects. 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:
我猜你 AllPermissions 的 Item 来源是一个 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. (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:
我假设您已经从一个基类继承了一个 RaisePropertyChanged 事件,以便在值更新时通知视图。(比如如果你使用mvvm light或其他框架为你提供,或者你可以自己写)另外如果你希望能够通过选中/取消选中复选框来编辑权限,那么记得设置绑定模式=TwoWay IE:
<ListBox
...
ItemsSource="{Binding AllPermissions}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding DisplayName}"
IsChecked="{Binding RoleHasPermission, Mode=TwoWay}"/>
</DataTemplate>
</ListBox.ItemTemplate>
回答by Andre
I would write a ViewModel for your Role that has a collection of Permissions.
我会为您的角色编写一个具有权限集合的 ViewModel。
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>
回答by Jordan Wilcken
I would try using the Converter property of the binding for IsChecked. The ConverterParameter for the binding would be set to the list of permissions for the selected role.
我会尝试使用 IsChecked 绑定的 Converter 属性。绑定的 ConverterParameter 将设置为所选角色的权限列表。
I don't know how familiar you are with ValueConverters, but I could write some code to illustrate if that would help. ValueConverter examples are easy to find on the internet. The first time I ever implemented a ValueConverter, I followed this example from David Anson's blog.
我不知道您对 ValueConverters 有多熟悉,但我可以编写一些代码来说明这是否有帮助。ValueConverter 示例在 Internet 上很容易找到。我第一次实现 ValueConverter 时,我遵循了 David Anson 博客中的这个示例。

