【发布时间】:2017-01-18 22:41:46
【问题描述】:
我有以下型号的 EF
public abstract class Shape
{
public int Id { get; set;}
public string Name { get; set;}
public string Color { get; set;}
}
public class Rectangle: Shape
{
public double Width { get; set;}
public double Height { get; set;}
}
public class Circle: Shape
{
public double Radius {get; set;}
}
public class Holder
{
public int Id { get; set;}
...
public int SomeProperty { get;set;}
...
public ObservableCollection<Shape> MyShapes { get; set;}
}
public class Context: DBContext
{
...
public DBSet<Shape> Shapes { get; set;}
public DBSet<Holder> Holders { get; set;}
}
在 WPF 应用程序中使用它的最佳方式是什么以及如何为数据网格设置 dataviewSource?
我想要类似的东西:
- 显示所有 Holder 的主数据网格(或列表框或树视图)
- 选择主DataGrid的项目时,第二个DataGrid显示所有矩形的MyShapes;第三个数据网格仅显示该项目的圆圈。
- 用户可以在数据网格上添加和删除行
例如我有代码隐藏
circleViewSource.Source =(holderViewSource.View.CurrentItem as Holder).MyShapes
rectangleViewSource.Source =(holderViewSource.View.CurrentItem as Holder).MyShapes
我有进入 xaml 的资源
<CollectionViewSource x:Key="circleViewSourceViewSource" d:DesignSource="{d:DesignInstance {x:Type Circle}, CreateList=True}"/>
<CollectionViewSource x:Key="rectangleViewSource" d:DesignSource="{d:DesignInstance {x:Type Rectangle}, CreateList=True}"/>
例如,当我想用第二个数据网格添加一个矩形时,我遇到了问题。例外是 datagrid 尝试创建抽象类 Shape 而不是 Rectangle。
我在数据库中有三个表(TPT 方法)(形状、矩形、圆形),因为我希望这两个形状具有不同的 ID 和名称。
我哪里做错了?
【问题讨论】:
标签: c# wpf entity-framework datagrid dataview