noo*_*and 8 c# wpf wpfdatagrid
我在我正在编写的WPF应用程序中有一些用户管理功能,并希望让最终用户更直观一些
我希望能够提供某种方法来轻松编辑给定用户所属的角色列表.此时网格由于绑定到a而被填充List<ApplicationUser>
ApplicationUser 是我自己的类定义为:
public class ApplicationUser
{
public Guid? UserId { get; set; }
public string GivenName { get; set; }
public string Surname { get; set; }
public string EmailAddress { get; set; }
public string UserPhone { get; set; }
public string NtLoginName { get; set; }
public List<Role> ApplicationRoles { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
可以看出,用户所处的角色保存在List<Role>.Role是我自己的类定义为:
public class Role
{
public Guid RoleId;
public string RoleName;
public string RoleDescription;
}
Run Code Online (Sandbox Code Playgroud)
下面的模型代表当前状态,我只是将角色作为List,并通过使用转换器只在gridview中显示角色作为换行符号的新字符串

然而,这是我想要实现的目标,以便更容易地切换各个组的成员资格.

现在我考虑一下,我可能不得不改变Role的定义以包含一个IsMember属性来促进对复选框的绑定,但如果有人有更好的方法我也会欢迎它.我可以在sproc中更改JOIN类型,因此我通过查询特定用户返回所有角色并相应地填充IsMember属性.
谢谢你的时间!
这是一段简短的代码,我已经为了帮助您而开始了.我假设您在创建应用程序用户时可以IsMember保持Role类的属性.通过在所有用户中拥有所有角色,我采取了最简单的方法(enum flags本来是最好的但是考虑到你的数据,我不确定这是没有管道的选项).我使用了最少的列来了解这个想法.如果INotifyPropertyChanged至少在Roles上实现,那么当前端的复选框发生变化时,您可以连接到通知并将其保留回数据库.
主要Xaml
<DataGrid DataContext="{StaticResource ResourceKey=AllUsers}" ItemsSource="{Binding}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding GivenName}" />
<DataGridTextColumn Binding="{Binding Surname}" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding ApplicationRoles}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding RoleName}" IsChecked="{Binding IsMember, Mode=TwoWay}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
数据Xaml
<x:Array x:Key="AllUsers" Type="Sample:ApplicationUser">
<Sample:ApplicationUser GivenName="Andrew" Surname="Fuller">
<Sample:ApplicationUser.ApplicationRoles>
<Sample:Role RoleName="Administrators" IsMember="True"/>
<Sample:Role RoleName="Shift Analysts"/>
<Sample:Role RoleName="Shift Managers" IsMember="True"/>
</Sample:ApplicationUser.ApplicationRoles>
</Sample:ApplicationUser>
<Sample:ApplicationUser GivenName="Anne" Surname="Dodsworth">
<Sample:ApplicationUser.ApplicationRoles>
<Sample:Role RoleName="Administrators"/>
<Sample:Role RoleName="Shift Analysts" IsMember="True"/>
<Sample:Role RoleName="Shift Managers" IsMember="True"/>
</Sample:ApplicationUser.ApplicationRoles>
</Sample:ApplicationUser>
</x:Array>
Run Code Online (Sandbox Code Playgroud)
类定义
public class ApplicationUser
{
public Guid? UserId { get; set; }
public string GivenName { get; set; }
public string Surname { get; set; }
public string EmailAddress { get; set; }
public string UserPhone { get; set; }
public string NtLoginName { get; set; }
public List<Role> ApplicationRoles { get; set; }
public ApplicationUser()
{
ApplicationRoles = new List<Role>();
}
}
public class Role
{
public Guid RoleId { get; set; }
public string RoleName { get; set; }
public string RoleDescription { get; set; }
public bool IsMember { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
结果
