简体   繁体   English

DataGridComboBoxColumn绑定到与DataGrid ItemsSource不同的列表

[英]DataGridComboBoxColumn binding to a different list that the DataGrid ItemsSource

So I am relatively new in WPF and a nice feature someone mentioned to me was the custom columns in datagrids.. So here is my question. 因此,我在WPF中相对较新,有人向我提到的一个不错的功能是datagrids中的自定义列。。所以这是我的问题。

I have two database tables, an Employee table and an Occupation table. 我有两个数据库表,一个雇员表和一个职业表。

Employee table 员工表

员工表

Occupation table 职业表

职业表

And as you can see I have a foreign key linking the two tables. 如您所见,我有一个链接这两个表的外键。 So in my app I set my DataGrid ItemsSource = list of Employees. 因此,在我的应用程序中,我设置了DataGrid ItemsSource =员工列表。 In my DataGrid I defined the columns myself, I disabled the AutoGenerateColumns property. 在DataGrid中,我自己定义了列,我禁用了AutoGenerateColumns属性。 I have 4 columns, 我有四列

0: TextColumn 0:TextColumn

1: TextColumn 1:TextColumn

2: TextColumn 2:TextColumn

3: ComboBoxColumn 3:ComboBoxColumn

So my question is, how can I set the ItemsSource of the ComboBoxColumn (4th column) to a list of my Occupation class, displaying the occupation description from the foreign key OccupationID? 所以我的问题是,如何将ComboBoxColumn(第4列)的ItemsSource设置为我的Occupation类的列表,以显示外键OccupationID的职业描述? and populating the combobox with all occupation descriptions? 并用所有职业描述填充组合框?

My code: 我的代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    List<Employee> employees;

    private void gridMain_Loaded(object sender, RoutedEventArgs e)
    {
        employees = EmployeeDataHandler.getAllEmployees();
        List<Occupation> occs = OccupationDataHandler.getAllJobs();
        dgEmployee.ItemsSource = employees;

    }        
}

class Employee
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public int Occupation { get; set; }
}

class Occupation
{
    public int ID { get; set; }
    public string Description { get; set; }
}

And my xaml code: 和我的XAML代码:

<Grid x:Name="gridMain" Loaded="gridMain_Loaded">
    <DataGrid x:Name="dgEmployee" HorizontalAlignment="Left" Height="301" Margin="10,10,0,0" VerticalAlignment="Top" Width="498" IsSynchronizedWithCurrentItem="True" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding ID}" ClipboardContentBinding="{x:Null}" Header="System ID"/>
            <DataGridTextColumn Binding="{Binding Name}" ClipboardContentBinding="{x:Null}" Header="Name"/>
            <DataGridTextColumn Binding="{Binding Surname}" ClipboardContentBinding="{x:Null}" Header="Surname"/>
            <DataGridComboBoxColumn ClipboardContentBinding="{x:Null}" Header="Occupation" SelectedValueBinding="{x:Null}" SelectedItemBinding="{x:Null}" TextBinding="{x:Null}"/>
        </DataGrid.Columns>
    </DataGrid>

</Grid>

Thanks so much for taking the time to read my question. 非常感谢您抽出宝贵的时间阅读我的问题。 PS This is all fake data so don't worry about the names in the screenshot PS这都是假数据,所以不用担心屏幕截图中的名称

Give the DataGridComboBoxColumn element an x:Key in your XAML markup and then set its ItemsSource property in your event handler: 在XAML标记中为DataGridComboBoxColumn元素提供x:Key ,然后在事件处理程序中设置其ItemsSource属性:

private void gridMain_Loaded(object sender, RoutedEventArgs e)
{
    employees = EmployeeDataHandler.getAllEmployees();
    List<Occupation> occs = OccupationDataHandler.getAllJobs();
    dgEmployee.ItemsSource = employees;
    cmb.ItemsSource = occs;
}

XAML: XAML:

<DataGridComboBoxColumn x:Name="cmb" ClipboardContentBinding="{x:Null}" 
                        Header="Occupation" 
                        SelectedValuePath="ID"
                        SelectedValueBinding="{Binding Occupation}"
                        DisplayMemberPath="Description "/>

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

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