WPF EntityFramework DataGrid ComboBox
13-Nov-1313 Leave a comment
There may be several ways of doing this:
<UserControl.Resources>
<CollectionViewSource x:Key="originatorViewSource" d:DesignSource="{d:DesignInstance my:Originator, CreateList=True}" />
<CollectionViewSource x:Key="customerLookup" />
</UserControl.Resources>
<DataGridComboBoxColumn x:Name="cboCustomer"
Width="200"
Header="Customer"
ItemsSource="{Binding Source={StaticResource customerLookup}}"
SelectedValueBinding="{Binding Path=CustomerID}"
SelectedValuePath="ID"
DisplayMemberPath="Customer1">
</DataGridComboBoxColumn>
private void LoadComboBox()
{
try
{
SalesContext contextCustomer = new SalesContext();
var customerList = from c in contextCustomer.Customers
orderby c.Customer1
select new { ID = c.ID, Customer1 = c.Customer1 };
CollectionViewSource CustomerViewSource = (CollectionViewSource)this.Resources["customerLookup"];
CustomerViewSource.Source = customerList.ToList();
//cboCustomer.ItemsSource = customerList.ToList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
End