简体   繁体   English

递延评估和DataGrid.ItemsSource出现问题

[英]Trouble with deferred evaluation and DataGrid.ItemsSource

I am experiencing an issue setting the ItemsSource property of a DataGrid to the result of a LINQ query. 我在将DataGrid的ItemsSource属性设置为LINQ查询的结果时遇到问题。 The exact error is: 确切的错误是:

Cannot access a disposed object. 无法访问已处置的对象。 Object name: 'DataContext accessed after Dispose.'. 对象名称:“在Dispose之后访问DataContext”。

Now, before you start snickering and whip off an answer regarding deferred query evaluation and disposing of your database contexts, know that I do understand all of that. 现在,在开始窃笑并得出有关延迟查询评估和处置数据库上下文的答案之前,请知道我确实了解所有这些内容。 I am calling ToList() on the result of the query and assigning that to the ItemsSource property. 我在查询结果上调用ToList()并将其分配给ItemsSource属性。 So, query executed, results read into memory, should be ok. 因此,执行查询,将结果读到内存中就可以了。

Yeah, not so much. 是的,不是很多。 At first I thought that there must be something about the property itself causing this, ie, some weird data binding thing that I don't know about (learning WPF and linq2Sql at the moment). 最初,我认为该属性本身一定存在某些原因,即我不知道的一些奇怪的数据绑定问题(此刻正在学习WPF和linq2Sql)。 After thinking some more about it, I still couldn't explain the problem as the DataGrid shouldn't have any notion of the DataContext, just the list (though returning a List from a test method and iterating through it later did not throw an exception). 在考虑了更多问题之后,我仍然无法解释该问题,因为DataGrid不应具有DataContext的任何概念,仅是列表(尽管从测试方法返回List并在之后进行遍历并没有引发异常) )。

It was the DataContext that was being accessed after its destruction, but that doesn't help me understand either because I am calling ToList() specifically to execute the query before the using block exits. 销毁后正在访问的是DataContext,但这也不帮助我理解,因为我正在调用ToList()专门用于在using块退出之前执行查询。 I can however solve the problem as shown below: 但是,我可以解决如下问题:

private void button1_Click( object sender, RoutedEventArgs e )
{
    using( NorthwindDataContext db = new NorthwindDataContext() )
    {
        db.DeferredLoadingEnabled = false;  // works, but why is it necessary at all?
        grid.ItemsSource = (from o in db.Orders
                            where o.CustomerID == "VINET"
                            select o).ToList();
    }
}

So yeah, I'd like to understand this behavior before moving on too far. 是的,我想先了解一下这种行为,然后再继续进行。 Thanks in advance. 提前致谢。

EDIT: xaml for the main window and DataGrid 编辑:xaml为主窗口和DataGrid

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="495" Width="722">
    <Grid>
        <DataGrid AutoGenerateColumns="true" Height="403" HorizontalAlignment="Left" Margin="12,41,0,0" Name="grid" VerticalAlignment="Top" Width="676" />
        <Button Content="Do it" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

Your code indicates that setting DeferredLoadingEnabled to false fixed this? 您的代码表明将DeferredLoadingEnabled设置为false可以解决此问题? If so I'd assume that your databinding was accessing properties of a related object, which, when accessed, was trying to fire off a new query to retrieve said object. 如果是这样,我假定您的数据绑定正在访问相关对象的属性,该对象在被访问时正试图触发新查询以检索该对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM