Bind WPF DataGrid to LINQ Query (Entity Framework)

2019-08-19 05:00发布

问题:

This must be very simple, but I seem to be missing something. I've searched around for a few hours without coming across anything that can resolve my problem. The issue is that although I can assign my LINQ query to a WPF DataGrid, when I try to edit one of the DataGrid's values I get the following error:

System.InvalidOperationException was unhandled Message='EditItem' is not allowed for this view. Source=PresentationFramework StackTrace: at System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(Object item) at System.Windows.Controls.DataGrid.EditRowItem(Object rowItem) at System.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs e) at System.Windows.Controls.DataGrid.OnExecutedBeginEdit(Object sender, ExecutedRoutedEventArgs e) at System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e)

The XAML for my DataGrid looks like this:

<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="565" HorizontalAlignment="Left" Margin="6,92,0,0" Name="translatedStringsDataGrid1" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="602">
    <DataGrid.Columns>
    <DataGridTextColumn x:Name="stringsIDColumn2" Binding="{Binding Path=StringsID}" Header="Strings Name" Width="SizeToHeader" />
    <DataGridTextColumn x:Name="translatedStringsValueColumn1" Binding="{Binding Path=TranslatedStringsValue}" Header="Translated Strings Value" Width="SizeToHeader" />
    </DataGrid.Columns>
</DataGrid>

I am doing a LINQ query in the SelectedChange event of a ComboBox like this:

private void cbSelectLang_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

    var query = from o in _context.TranslatedStrings.Local 
                where o.LanguagesID == cbSelectLang.SelectedIndex + 1
                join r in _context.Strings.Local on o.StringsID equals r.StringsID into SubSet2

                from s in SubSet2.DefaultIfEmpty()

                select new { StringsID = s.StringsName, TranslatedStringsValue = o.TranslatedStringsValue };

    this.translatedStringsDataGrid1.ItemsSource = query;

}

I'm using "POCO entities" if anybody thinks there is an easier way of accomplishing this. I really do get the feeling that I'm missing something very basic and obvious, if anybody would be so kind as to point it out to me! :-)

Many thanks.

回答1:

I haven't tested this, but I fairly sure your problem is because you're returning an anonymous type from your query. Try changing it to

...
from s in SubSet2.DefaultIfEmpty()
select new MyRealType 
{ 
    StringsID = s.StringsName, 
    TranslatedStringsValue = o.TranslatedStringsValue 
};

where you need to define MyRealType.



回答2:

Thanks partly to Phil I now have a workable technique which involves an ObservableCollection and a new holder type:

    private class JoinClass
    {
        public string StringsID { get; set; }
        public string TranslatedStringsValue { get; set; }
    }

    private void cbSelectLang_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ObservableCollection<JoinClass> collection = new ObservableCollection<JoinClass>();

        var query = from o in _context.TranslatedStrings.Local
                                where o.LanguagesID == cbSelectLang.SelectedIndex + 1
                                join r in _context.Strings.Local on o.StringsID equals r.StringsID into SubSet
                                from s in SubSet.DefaultIfEmpty()
                                select new JoinClass { StringsID = s.StringsName, TranslatedStringsValue = o.TranslatedStringsValue };

        foreach (var item in query)
        {
            collection.Add(item);
        }

        this.translatedStringsDataGrid1.ItemsSource = collection;
    }

Thank you!