WPF如何在ComboBox中显示DataGrid的TextBlock中的关系数据

时间:2016-09-16 19:14:51

标签: c# wpf xaml datagrid

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

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

Related Tables

能够让组合框显示关联RoleIdFk的Role.RoleName。

ComboBoxes

在我的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:

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

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

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

这就是网格的样子:

DataGrid

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

有没有办法在XAML中实际执行此操作,或者我是否需要设置某种值转换器来实现此目的?

注意:我曾考虑使用某种“切换/案例”方案。虽然它可以在这里工作,但我需要找出一些更有活力的东西。在项目的后期,我需要将PersonnelId与其他项目相关联。对于300多人来说,“切换/案例”场景是不可行的。

1 个答案:

答案 0 :(得分:0)

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

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

如果您没有引用的对象,最好连接两个集合并将结果用作DataGrid的ItemsSource。这是一个例子: Joining two tables in one datagrid usig Linq C# WPF

IMultiValueConverter也有一个解决方案,但似乎过于复杂: Joining 2 collections into datagrid using multibinding