简体   繁体   English

增强WPF网格视图中特定列的显示

[英]Enhancing the display of a particular column in a WPF grid view

I have some user admin functionality in a WPF app that I'm currently writing and would like to make it a bit more intuitive for the end user 我在我正在编写的WPF应用程序中有一些用户管理功能,并希望让最终用户更直观一些

I'd like to be able to provide some kind of means of easily editing the list of roles a given user belongs to. 我希望能够提供某种方法来轻松编辑给定用户所属的角色列表。 At the moment the grid is filled as a result of binding to a List<ApplicationUser> 此时网格由于绑定到List<ApplicationUser>而被填充

ApplicationUser is my own class defined as: 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; }
}

As can be seen the roles that the user is in are held in a List<Role> . 可以看出,用户所处的角色保存在List<Role> Role is my own class defined as: Role是我自己的类定义为:

public class Role
{
   public Guid RoleId;
   public string RoleName;
   public string RoleDescription;
}

The below mockup represents the current state where I just get the roles as a List and through the use of a converter just display the roles as new-line separated strings in the gridview 下面的模型代表当前状态,我只是将角色作为List,并通过使用转换器只在gridview中显示角色作为换行符号的新字符串

gridview的当前状态

However this is what I'd like to achieve to make toggling off and on membership of various groups easier. 然而,这是我想要实现的目标,以便更容易地切换各个组的成员资格。

期望的gridview状态

Now that I think about it I'll probably have to change the definition of Role to include an IsMember property to facilitate binding on the checkbox but if anybody has a better way I'll welcome that as well. 现在我考虑一下,我可能不得不改变Role的定义以包含一个IsMember属性来促进对复选框的绑定,但如果有人有更好的方法我也会欢迎它。 I can change the JOIN type in the sproc so I get back all roles with a query about a particular user and fill the IsMember property accordingly. 我可以在sproc中更改JOIN类型,因此我通过查询特定用户返回所有角色并相应地填充IsMember属性。

Thanks for your time! 谢谢你的时间!

Here is a short piece of code I've whipped up to get you started. 这是一段简短的代码,我已经为了帮助您而开始了。 I've assumed that you can hydrate IsMember property of Role class when you create the application user. 我假设您在创建应用程序用户时可以IsMember Role类的IsMember属性。 I've taken the easiest route by having all roles in all users ( enum flags would have been best but given your data, I'm not sure that's an option without some plumbing). 通过在所有用户中拥有所有角色,我采取了最简单的方法( enum flags最好但是根据您的数据,我不确定这是没有管道的选项)。 I've used minimal columns to get the idea across. 我使用了最少的列来了解这个想法。 If you implement INotifyPropertyChanged on Roles at least, you can hook up to the notification and persist it back into database when the check-boxes change on front end. 如果至少在Roles上实现INotifyPropertyChanged ,则可以在前端更改复选框时挂接通知并将其保留回数据库。


Main Xaml 主要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>

Data Xaml 数据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>

Class definitions 类定义

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

Result 结果

截图

If the Roles column always displays the same list of roles, you can easily bind a ListView to the list of all roles with an ItemTemplate that is built from a CheckBox and a TextBlock. 如果Roles列始终显示相同的角色列表,则可以使用从CheckBox和TextBlock构建的ItemTemplate轻松地将ListView绑定到所有角色的列表。
Then, you can easily bind the IsChecked property of the CheckBox to the User Roles and use a converter that returns True if the role is in the user Roles list. 然后,您可以轻松地将CheckBox的IsChecked属性绑定到用户角色,并使用如果角色位于用户角色列表中则返回True的转换器。

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

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