I have a wpf application (MVVM style) that has a datagrid. I am using entity framework as my model. In this datagrid I have a column that comes from a value in another table. I use navigation properties to get this value. When the datagrid loads the column in question displays the right value, but there is still a binding error being generated in the output window:
System.Windows.Data Error: 40 : BindingExpression path error: 'product_category' property not found on 'object' ''tracking_stock_EA7F46EC8AD7F155921357AA6714C6C20BE807C4760A3B6DFC4FAC1954CA8119' (HashCode=49385318)'. null
The object that the datagrid row is bound to is called a tracking_stock which has a navigation property of product, which itself has a navigation property of products_category which i use to get the product_category.chrProdCtgry
Here is an image to outline the EF design:
so the binding for my textblock inside a datagrid template column is as follows:
<DataGridTemplateColumn Header="CATEGORY" SortMemberPath="product.product_category.chrProdCtgry" ToolTipService.ToolTip="Category of the product" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding product.product_category.chrProdCtgry}" Style="{StaticResource FTC_DetailLabelSub}" Width="120" TextWrapping="NoWrap"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Now, everything works, the column can be sorted, the text in the textblock has the value it is supposed to.
QUESTION:
Why is this binding error being thrown when everything works properly?
Edit # 1:
I tried to remove the sort declaration:
SortMemberPath="product.product_category.chrProdCtgry"
The binding error is still there
Edit # 2:
So I tried adding the IsAsync=True to both the textblock and collection binding for the datagrid itself. Now the textblock value does not show up until I sort the category column. In addition the binding errors listed above are still present, but now a new one is generated:
System.Windows.Data Error: 17 : Cannot get 'product_category' value (type 'product_category') from 'product' (type 'product_83A5741ABC5EE6395B89B7B4B384137E8CF95991EF9918BD4525EF06B774469E'). BindingExpression:Path=product.product_category.chrProdCtgry; DataItem='tracking_stock_EA7F46EC8AD7F155921357AA6714C6C20BE807C4760A3B6DFC4FAC1954CA8119' (HashCode=2011918); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') TargetInvocationException:'System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
Edit #3:
Sherridan's post got me thinking about the data not being there when the datagrid's collection is created from the dataBase context. So I went back to the entity framework "GET" function and forced an include of the navigation properties in the following three ways, none of which got rid of the binding error:
1
Dim trackingList = Await Context.tracking_stock.Include("product").ToListAsync
Return New ObservableCollection(Of tracking_stock)(trackingList)
2
Dim trackingList = Await Context.tracking_stock.Include("product.product_category").ToListAsync
Return New ObservableCollection(Of tracking_stock)(trackingList)
3
Dim trackingList = Await Context.tracking_stock.Include("product").Include("product.product_category").ToListAsync
Return New ObservableCollection(Of tracking_stock)(trackingList)