【问题标题】:Bind WPF DataGrid to LINQ Query (Entity Framework)将 WPF DataGrid 绑定到 LINQ 查询(实体框架)
【发布时间】:2012-05-21 15:14:59
【问题描述】:

这一定很简单,但我似乎遗漏了一些东西。我已经搜索了几个小时,没有遇到任何可以解决我的问题的东西。问题是,虽然我可以将我的 LINQ 查询分配给 WPF DataGrid,但当我尝试编辑 DataGrid 的值之一时,我收到以下错误:

System.InvalidOperationException 未处理 此视图不允许使用 Message='EditItem'。 来源=演示框架 堆栈跟踪: 在 System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(对象项) 在 System.Windows.Controls.DataGrid.EditRowItem(对象 rowItem) 在 System.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs e) 在 System.Windows.Controls.DataGrid.OnExecutedBeginEdit(对象发送者,ExecutedRoutedEventArgs e) 在 System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e)

我的 DataGrid 的 XAML 如下所示:

<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 查询,如下所示:

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 实体”。我真的觉得我错过了一些非常基本和明显的东西,如果有人愿意向我指出的话! :-)

非常感谢。

【问题讨论】:

    标签: linq entity-framework wpf-controls ef-code-first


    【解决方案1】:

    我没有对此进行测试,但我相当确定您的问题是因为您从查询中返回了匿名类型。尝试将其更改为

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

    您需要在哪里定义 MyRealType。

    【讨论】:

    • 是的,谢谢菲尔,这是我在下面给出的答案的一部分
    【解决方案2】:

    部分感谢 Phil,我现在有了一个可行的技术,其中涉及 ObservableCollection 和新的持有者类型:

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

    谢谢!

    【讨论】:

    • 你可以更简单地做 "var collection=new ObservableCollection(query.ToList());"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 2011-01-08
    • 1970-01-01
    相关资源
    最近更新 更多