如何将查询生成的泛型列表返回到DataGrid ItemSource

本文关键字:返回 列表 DataGrid ItemSource 泛型 查询 | 更新日期: 2023-09-27 18:25:39

我正在从实体模型生成一个DataGrid,这在后面的代码中非常简单。但现在我正试图将其绑定到MVVM模式中,我似乎不知道如何返回泛型列表。

从模型视图:

    public ??? LoadMoviesMethod()
    {
        ObjectQuery<MovieTable> _movies = dataEntities.MovieTables;
        var query =
            from MovieTable in _movies
            where MovieTable.Rating == "R"
            orderby MovieTable.id
            select new { MovieTable.Title, MovieTable.Rating, MovieTable.Stars, MovieTable.ReleaseYear };
       ??? MoviesList = query.ToList();
       return MoviesList;
//where as the code behind was simply gridName.ItemsSource = query.ToList()
    }

发送到数据网格:

<UserControl x:Class="MovieMockup.MovieMockupListView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d"
         d:DesignHeight="300" 
         d:DesignWidth="300">
<DataGrid ItemSource="{Binding MovieMockupListModel.MoviesList}">
</DataGrid>
</UserControl>

如有任何意见,我们将不胜感激!!

如何将查询生成的泛型列表返回到DataGrid ItemSource

我通常使用IListIEnumerable,尽管我想您也可以返回object