【问题标题】:EF6 inheritance and WPF dataViewSource for collection of abstract classEF6 继承和 WPF dataViewSource 用于抽象类的集合
【发布时间】: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?

我想要类似的东西:

  1. 显示所有 Holder 的主数据网格(或列表框或树视图)
  2. 选择主DataGrid的项目时,第二个DataGrid显示所有矩形的MyShapes;第三个数据网格仅显示该项目的圆圈。
  3. 用户可以在数据网格上添加和删除行

例如我有代码隐藏

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


    【解决方案1】:

    我在数据网格中禁用了 CanUserAddRows

    CanUserAddRows="False"

    并添加了两个按钮:第一个按钮添加新的 Rectangle,第二个按钮将新的 Circle 添加到所选 Holder 的 MyShapes 集合中。

    我还删除了与holderViewSource 的 CurrentItem 相关的 code.behind 部分。一切都可以通过绑定和 DataTemplate 在 xaml 中完成。

    【讨论】:

      猜你喜欢
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多