I'm trying to display foreing key values in WPF DataGrid. Problem is when I load the window, the datagrid field stays empty. But when I insert or update data(operation happens in separate window), and the datagrid is refreshed, the values are displayed properly.
This is how i bind it:
<DataGridTextColumn Header="Team" Binding="{Binding Team.TeamName}"/>
DataGrid control is in EmployeesWindow.xaml
This is the table with the foreign key:
public class Employee
{
[Key]
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
//..
[ForeignKey("Team")]
public int TeamId { get; set; }
public Team Team { get; set; }
}
And this is the table that is being referenced
public class Team
{
[Key]
public int TeamId { get; set; }
public string TeamName { get; set; }
public int TeamBonus { get; set; }
}
In EmployeesWindow.xaml
:
public static DataGrid datagrid;
public EmployeesWindow()
{
InitializeComponent();
datagrid = EmployeeDataGrid;
datagrid.ItemsSource = _db.Employees.ToList();
}
In TeamsWindow.xaml
:
private void AddEmployee_Click(object sender, RoutedEventArgs e)
{
//..
EmployeesWindow.datagrid.ItemsSource = _db.Employees.ToList();
this.Hide();
}
How can I display the foreign key value in WPF DataGrid data-binding?