gpt4 book ai didi

wpf - MVVM/WPF 应用 RadGridView 绑定(bind)到 EntityFrameworkDataSource

转载 作者:行者123 更新时间:2023-12-03 10:44:59 27 4
gpt4 key购买 nike

我正在尝试将 RadGridView 绑定(bind)到 EntityFrameworkDataSource,这样我就可以避免自己编写分页/过滤,但我无法让它工作。我已经检查了 Telerik 的示例,但它似乎也不起作用。使用带 EF 6 的 WPF

我的背景:

public class TIMSContext : DbContext
{
public TIMSContext()
: base("name=TIMSContext") { }


protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//map models here...
}

public IQueryable<T> Query<T>()
{
return this.Set(typeof(T)).AsQueryable() as IQueryable<T>;
}

}

我的 XAML:
     <telerik:RadGridView x:Name="RadGridView1"
Grid.Row="0"
AlternateRowBackground="CornflowerBlue"
AlternationCount="2"
AutoGenerateColumns="True"
CanUserDeleteRows="False"
CanUserFreezeColumns="False"
CanUserInsertRows="False"
GroupRenderMode="Flat"
IsReadOnly="True"
ItemsSource="{Binding ShipmentData}"
RowIndicatorVisibility="Collapsed" />

<telerik:RadDataPager Grid.Row="1"
DisplayMode="FirstLastPreviousNextNumeric"
PageSize="25"
Source="{Binding ShipmentData}" />

我的 View 模型:
public class ShipmentsViewModel :ViewModelBase
{
private readonly QueryableEntityCollectionView<TIMS.Data.Entities.ASN> shipments;
private readonly TIMS.Data.TIMSContext ctx;

public ShipmentsViewModel()
{
try
{
ctx = new Data.TIMSContext();
shipments = new QueryableEntityCollectionView<Data.Entities.ASN>(ctx, "ASN");
//Argument 1: cannot convert from 'TIMS.Data.TIMSContext' to 'System.Data.Objects.ObjectContext'


//Also tried this which I saw on some forums...
//shipments = new QueryableEntityCollectionView<Data.Entities.ASN>( ((IObjectContextAdapter)ctx).ObjectContext, "ASN");
//Argument 1: cannot convert from 'System.Data.Entity.Core.Objects.ObjectContext' to 'System.Data.Objects.ObjectContext'

}
catch (Exception)
{
}
}

#region Properties

public Object ShipmentData
{
get
{
return shipments;
}
}

#endregion

}

最佳答案

原来EF 6有一个特定的程序集......需要引用Telerik.Windows.Controls.EntityFramework60。并且必须将 DbContext 转换为 IObjectContextAdapater,如上面的代码中所示。

此外,您必须在上下文中为每个 DbSet 显式定义属性,并在进行函数调用时提供名称。这部分有点烦人...

关于wpf - MVVM/WPF 应用 RadGridView 绑定(bind)到 EntityFrameworkDataSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30380843/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com