WPF如何像在ComboBox中一样在DataGrid的TextBlock中显示关系数据 [英] WPF How to display relational data in DataGrid's TextBlock like in ComboBox

查看:55
本文介绍了WPF如何像在ComboBox中一样在DataGrid的TextBlock中显示关系数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在寻求有关此主题的帮助时,我通常会找到有关如何用相关数据填充ComboBox的信息(在一个表中显示源表中外键的名称),但是我已经能够做到.我正在寻找有关如何在TextBlock中实现相同结果的解决方案.

When searching for help on this topic, I typically find information on how to populate a ComboBox with related data (display name in one table for the foreign key in originating table), but I am already able to do that. I am looking for a solution on how to achieve the same result in a TextBlock.

在我的项目中,我使用EntityFramework创建了到SQL数据库的链接.有两个表:人员和角色.它们与Personnel.RoleIdFk和Role.RoleId相关.

In my project I have created a link to an SQL database using EntityFramework. There are two tables: Personnel and Role. They are related with Personnel.RoleIdFk and Role.RoleId.

有能力获得一个组合框,以显示关联的RoleIdFk的Role.RoleName.

I am able to get a combobox to display the Role.RoleName for the associated RoleIdFk.

在我的ViewModel中:

    private ICollection<Role> roleList;
    public ICollection<Role> RoleList
    {
        get { return roleList; }
        set { roleList = value; NotifyPropertyChanged("RoleList"); }
    }

    private Role selectedRole;
    public Role SelectedRole
    {
        get { return selectedRole; }
        set { selectedRole = value; NotifyPropertyChanged("SelectedRole"); }
    }

    public void SetSelectedRole()
    {
        if (SelectedPerson == null)
        {
            SelectedPerson = PersonnelList.Select(p => p)
                                          .FirstOrDefault();
        }


        SelectedRole = RoleList.Where(p => p.RoleId == SelectedPerson.RoleIdFk)
                                   .FirstOrDefault();
    }

以及ComboBox的 XAML:

And the XAML for the ComboBox:

        <ComboBox x:Name="cboRole"
              Grid.Column="1" Grid.Row="0"
              Width="150" Height="35"
              ItemsSource="{Binding RoleList}"
              DisplayMemberPath="RoleName"
              SelectedItem="{Binding SelectedRole}"
              />

我的问题是我还在为人员表显示一个DataGrid.在此DataGrid中,我希望显示关联的RoleName而不是数字外键.
当前 DataGrid的XAML:

My problem is I am also displaying a DataGrid for the Personnel table. In this DataGrid, I would like to have the associated RoleName displayed instead of the numerical foreign key.
The current XAML for the DataGrid:

<Window.DataContext>
    <viewModel:MainWindowVM />
</Window.DataContext>

    <ListView Name="lstPersonnel"
              Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1"
              ItemContainerStyle="{StaticResource ItemContStyle}"
              ItemsSource="{Binding PersonnelList}" >
        <ListView.View>
            <GridView>
                <GridViewColumn Width="75" Header="First Name" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding FirstName}" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

                <GridViewColumn Width="75" Header="Last Name" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding LastName}" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

                <GridViewColumn Width="75" Header="Role" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding RoleIdFk}" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

这是网格的样子:

对于最后一列,我尝试以各种方式更改文本字段.我也尝试过使用其他模板和样式.这些都没有成功.

For the final column, I have tried altering the text field in various ways. I have also tried using other Templates and Styles. None of these have been successful.

是否有办法在XAML本身中实际执行此操作,或者我是否必须设置某种值转换器来完成此操作?

Is there a way to actually perform this in XAML itself, or am I going to have to set up some sort of value converter to accomplish this?

注意:我曾考虑过使用某种开关/案例"方案.尽管它在这里可以工作,但我需要找出一些可以更加动态化的东西.在项目的稍后部分,我将需要将PersonnelId与其他项目相关联.对于300名以上的人员来说,切换/案例"方案是不可行的.

NOTE: I had thought of using some sort of "switch/case" scenario. While it would work here, I need to figure out something that can be more dynamic. Later in the project I will need to associate the PersonnelId with other items. A "switch/case" scenario is not feasible with 300+ people.

推荐答案

角色"应该在人员"对象中可用.因此,您只需使用"Role.RoleName"而不是"RoleIdFk"即可:

"Role" should be available in "Personnel" object. So you can simply use "Role.RoleName" instead of "RoleIdFk":

<TextBlock Text="{Binding Role.RoleName}" />

在没有引用对象的情况下,最好将两个集合连接起来并使用结果作为DataGrid的ItemsSource.这是一个例子:在一个datagrid中连接两个表usig Linq C#WPF

In cases when you don't have a referenced object, it's better to join two collections and use a result as an ItemsSource of a DataGrid. Here is an example: Joining two tables in one datagrid usig Linq C# WPF

还有一个使用IMultiValueConverter的解决方案,但似乎过于复杂:使用多重绑定将2个集合加入到datagrid中

There is also a solution with IMultiValueConverter, but it seems overcomplicated: Joining 2 collections into datagrid using multibinding

这篇关于WPF如何像在ComboBox中一样在DataGrid的TextBlock中显示关系数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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