Hi All,
I'm stuck, and have been through many tutorials and forums but i'm unable to solve my problem. i really appreciate any help i can get since i've been stuck for so long.
My problem is related to wpf databinding to Entity framework look up tables.
OBJECTIVE
To bind a wpf datagrid to an Entity framework db; some columns are to be of relationships to look up tables as shown below.
By look up table, (referring to the EF code below); i want each EQUIPMENT to be able to select only one LOCATION, but locations are just strings not objects.
I want each equipment entry to have a combobox to select location which will then store back to the database with a save changes (or automatically)
EF Database
class EF_Database : DbContext { // Table Objects public DbSet<Equipment> EquipmentReg { get; set; } public DbSet<Location> Locations { get; set; } // Look Up Table } [Table("EquipmentRegister")] // A Data Annotation to override the name given ClassName(plural) default (I.e. "Equipments") class Equipment { // Primary Key public int Id { get; set; } public string Tag { get; set; } //TODO Indexing views etc public virtual Location Location { get; set; } // Look up table } class Location { public int Id { get; set; } public string Name { get; set; } }
HOW SHOULD THIS BE DONE? I.e. How can i display the relationship member for each row?
Below is what i feel my best attempts of the XAML.
------------------------------------------------------------------------------------------------------------
DATAGRID
By following the drag and drop method from datasources; by default in the data sources window, the LOCATION shows the none icon. If i change this to a combobox, it will create a cell template that points to LOCATION (i then add .Name to the end)
<DataGridTemplateColumn x:Name="locationColumn" Header="Location" Width="SizeToHeader"><DataGridTemplateColumn.CellTemplate><DataTemplate><ComboBox><ComboBoxItem Content="{Binding Location.Name}"/></ComboBox></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn>
This doesn't work it creates a combo box that pulls no values.
So closest forum post could find led me to try below which doesn't even show a combo box.
<DataGridComboBoxColumn Header="Location" ItemsSource="{Binding Source={StaticResource equipmentViewSource}}" SelectedValuePath="Id" DisplayMemberPath="Name" SelectedValueBinding="{Binding Location}" />
Also note; i originally was using a foreign key member for LOCATION in EQUIPMENT; but this cause issues with the database not being able to add rows etc. Currently the database works with the EF code above.
Thanks in advance for any help!