As far as I understand, WPF should link to a nested property through a dot, but nevertheless, I have an empty field in the DataGrid: empty name field The following is a XAML:

<DataGrid x:Name="storageGrid" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" AutoGenerateColumns="False" Height="351" Width="643"> <DataGrid.Columns> <DataGridTextColumn Header="Name" Binding="{Binding Product.Name}" Width="100" /> <DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" Width="100" /> </DataGrid.Columns> </DataGrid> 

I use classes generated by the Entity Framework 6, Database First approach. Below is the definition of Storage:

 public partial class Storage { public Nullable<int> ProductId { get; set; } public Nullable<int> Quantity { get; set; } public Nullable<int> RoomId { get; set; } public int EntryId { get; set; } public virtual Product Product { get; set; } public virtual Room Room { get; set; } } } 

Here is how I take Storage from the database:

  public void InitializeData() { storageGrid.Items.Clear(); var dbStorages = Repository.Storages(); foreach (var storage in dbStorages) { Storages.Add(storage); } storageGrid.ItemsSource = Storages; storageGrid.Items.Refresh(); } 

How to force WPF to correctly display the Product.Name field?

    1 answer 1

    The problems were with lazy loading, but when I turned it off, authorization flew away, so I changed the Repository.Storages () as follows:

     return ctx.Storages.Include(s => s.Product).ToList(); 

    Well, in yuzingah added

     using System.Data.Entity; 

    To get the extension of the Include method with the lambda ^^