将WPF DataGrid绑定到LINQ查询(实体框架) [英] Bind WPF DataGrid to LINQ Query (Entity Framework)

查看:150
本文介绍了将WPF DataGrid绑定到LINQ查询(实体框架)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这必须非常简单,但是我似乎缺少了一些东西.我搜索了几个小时,没有发现任何可以解决我的问题的东西.问题是,尽管我可以将LINQ查询分配给WPF DataGrid,但是当我尝试编辑DataGrid的值之一时,却出现以下错误:

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未处理 此视图不允许使用Message ='EditItem'. 来源= PresentationFramework 堆栈跟踪: 在System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(对象项) 在System.Windows.Controls.DataGrid.EditRowItem(Object rowItem) 在System.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs e) 在System.Windows.Controls.DataGrid.OnExecutedBeginEdit(对象发送者,ExecutedRoutedEventArgs e) 在System.Windows.Input.CommandBinding.OnExecuted(Object sender,ExecutedRoutedEventArgs e)

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)

我的DataGrid的XAML看起来像这样:

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>

我正在像这样的ComboBox的SelectedChange事件中进行LINQ查询:

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;

}

如果有人认为有实现此目的的简便方法,那么我使用的是"POCO实体".如果有人愿意向我指出这一点,我确实真的感到自己缺少了一些非常基本和显而易见的东西! :-)

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! :-)

非常感谢.

推荐答案

我尚未对此进行测试,但是我很确定您的问题是因为您要从查询中返回匿名类型.尝试将其更改为

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 
};

您需要在其中定义MyRealType的地方.

where you need to define MyRealType.

这篇关于将WPF DataGrid绑定到LINQ查询(实体框架)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆