M
M
Maxim Isaev2019-02-16 16:13:30
WPF
Maxim Isaev, 2019-02-16 16:13:30

How to bind table data to combobox?

Good afternoon, there are 2 tables in the database, they are presented in the project like this:

Model
public class Employee
    {
        public int Id { get; set; }
        public string Surname { get; set; }
        public string Name { get; set; }
        public string Patronymic { get; set; }
        public DateTime BirthDate { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public int TitleId { get; set; }
        public Title Title { get; set; }
    }

public class Title
    {
        public int Id { get; set; }

        [Column("Title")]
        [NotMapped]
        public string Name { get; set; }
        public List<Employee> Employees { get; set; }
    }


It is necessary that all employees in the "Position" (Title) field instead of an identifier have a specific value and it can be changed by selecting a different position from the drop-down list.
My thoughts.
First of all, you need to write all the data from the table "Position" (Title) to the collection. How and where to do it? Create a separate class?
And the next thing is to tie something like this?
<DataGridComboBoxColumn ItemsSource="{Binding ListTitles}"
                                        DisplayMemberPath="Name"
                                        SelectedValuePath="{Binding Path=TitleId}"
                                         Header="Должность"/>

Where ListTitles is a collection of posts

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Foggy Finder, 2019-02-16
@FoggyFinder

All right, although you might prefer the standard ComboBox over the
DataGridComboBoxColumn :

<DataGridTemplateColumn Header="Должность">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox
                 HorizontalContentAlignment="Center"
                 ItemsSource="{Binding DataContext.Titles, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                 SelectedItem="{Binding Title}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Be careful, the above example assumes that the Titles
collection is at the same level (class) as the Employee collection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question