【问题标题】:WPF How to display relational data in DataGrid's TextBlock like in ComboBoxWPF 如何像在 ComboBox 中一样在 DataGrid 的 TextBlock 中显示关系数据
【发布时间】:2016-09-16 19:14:50
【问题描述】:

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

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

能够获得一个组合框来显示关联 RoleIdFk 的 Role.RoleName。

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

这是网格的样子:

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

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

注意:我曾想过使用某种“开关/案例”场景。虽然它可以在这里工作,但我需要找出一些更动态的东西。稍后在项目中,我需要将 PersonnelId 与其他项目相关联。超过 300 人的“切换/案例”场景是不可行的。

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案1】:

    “角色”应该在“人员”对象中可用。所以你可以简单地使用“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

    【讨论】:

    • 天啊。我想我的问题是我没有关注 K.I.S.S.校长和我试图让它过于复杂。效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 2021-11-16
    • 2011-10-22
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多