I have a WPF application in which I use entity framework with mysql connector/net to obtain data back. I have a combobox which is filled with items from a table. A datagrids itemssource property is binded to the combobox' selecteditem.staff property, which displays the data correctly. The problem is that I am unable to sort this data. If I bind the data directly to the datagrid, the sorting works correctly. However if I bind the data to the combobox, then bind the datagrid to the selected item of the grid, the sorting is lost.
Entity Framework Data Retrieval
private void RefreshOffices()
{
try
{
using (var context = new LDC_Entities())
{
cmbOffice.ItemsSource = context.Offices.Include("Staff.Roles").Include("Manager").ToList();
}
}
catch (Exception eX)
{
MessageBox.Show(String.Format("Unable to retrieve offices\nError:{0}", eX.Message),"Error",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
XAML Binding
<ComboBox Name="cmbOffice" Width="150" SelectedIndex="0">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}({0}) - {1}">
<Binding Path="Office_ID"/>
<Binding Path="City"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<DataGrid Grid.Row="1" Margin="10,50,10,10" Name="displayGrid" IsReadOnly="True" AutoGenerateColumns="False"
ItemsSource="{Binding ElementName=cmbOffice, Path=SelectedItem.Staff}">
<DataGrid.Columns>
<DataGridTextColumn Header="Staff ID" Binding="{Binding Staff_ID}"/>
<DataGridTextColumn Header="Name" Width="120" Binding="{Binding Full_Name}" />
<DataGridTextColumn Header="Address" Width="140" Binding="{Binding Address}" />
<DataGridTextColumn Header="DOB" Width="80" Binding="{Binding DOB, StringFormat={}{0:dd/MM/yyyy}}" />
<DataGridTextColumn Header="Telephone" Width="100" Binding="{Binding Telephone}" />
<DataGridTextColumn Header="Roles" Width="*" Binding="{Binding RolesList}" />
</DataGrid.Columns>
</DataGrid>
Any help would be much appreciated, I have tried playing with the CanUserSort property of the datagrid to no success. I believe it is to do with how the datagrid is filled not by the selected item, but by the .staff collection of staff members.
Thanks in advance, Mike