【问题标题】:Bind additonal elements In ICollection<Model> Without breaking EF EDMX在 ICollection<Model> 中绑定附加元素而不破坏 EF EDMX
【发布时间】:2018-08-13 14:27:53
【问题描述】:

我无法使用MVVM 方法将数据绑定到DataGrid。可能很傻,但我被困住了。

我有一个 EF6 表 EFTable,看起来像这样

namespace Database.Models
{    
    [Table(Name = "EFTable")]
    public class EFTable
    {
        [Column(IsPrimaryKey = true)]
        [Key]
        public int ID { get; set; }

        public int Col1 { get; set; }

        public int Col2 { get; set; }

        .................
        ...................

    }
}


在我的 ViewModel 中有

 public ICollection<EFTable> EFTableToBindWithDataGrid
 {
     get
     {
         return efTable;
     }
     set
     {
         efTable = value;

         RaisePropertyChanged("EFTableToBindWithDataGrid");
     }
 }


最后在我的 View(xaml) 中我有一个 DataGrid

<DataGrid   ColumnWidth="*"  AutoGenerateColumns="False" ItemsSource="{ Binding Path=EFTableToBindWithDataGrid,Mode=OneWay}" >    
    <DataGrid.Columns>    
        <DataGridTextColumn Header="Col1" Binding="{Binding Col1}"/>
        <DataGridTextColumn Header="Col2" Binding="{Binding Col2}" />
        <!--................-->
         <!--#####HERE-->
        <DataGridTextColumn Header="ColNotInEFTable" Binding="{Binding ColNotInEFTable}" />

    </DataGrid.Columns>   
</DataGrid>


让我说说我的问题

  1. 我需要绑定计算值ColNotInEFTable,但它们不在EFTableToBindWithDataGrid中(因为EFTable没有ColNotInEFTable字段)
  2. 是否有其他方法可以解决此问题?



提前致谢

【问题讨论】:

  • computed values CGST and SGST but they are not in AddedInvoiceProducts。你不认为这些代码应该是minimal reproducible example 的一部分吗?
  • 这只是一个例子。
  • 好的,太好了。回答您的问题(“是否有其他方法可以解决此问题?”):是
  • 我用一个通用的例子更新了这个问题

标签: c# wpf mvvm entity-framework-6


【解决方案1】:

从您的Model 创建一个ViewModel

public class ViewModelInvoiceProduct : INotifyPropertyChanged
{
   //only the properties in Model that you want to show in DataGrid

   //your additional property
   private int _CGST;
   public int CGST
    {
        get
        {
            return _CGST;
        }
        set
        {
            _CGST = value;
            RaisePropertyChanged("CGST");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

VIEWMODEL:并将AddedInvoiceProductsViewModel 绑定到您的DataGrid

public ICollection<ViewModelInvoiceProduct> AddedInvoiceProductsViewModel {get;set;}

public ICollection<InvoiceProducts> AddedInvoiceProducts
    {
        get
        {
            return invoiceProducts;
        }
        set
        {
            invoiceProducts = value;
            //the idea to populate AddedInvoiceProductsViewModel
            foreach(var invoice in invoiceProducts)
            {
                var temp = new ViewModelInvoiceProduct();
                temp.XXX = invoice.XXX;
                temp.CGST = 1 + 2;
                AddedInvoiceProductsViewModel.Add(temp);
            }
        }
    }

【讨论】:

  • InvoiceProducts,想法是创建一个从 Model 克隆的 ViewModel 列表
【解决方案2】:
  1. 您可以扩展您的 InvoiceProducts 类:将其标记为部分类并将新属性添加到放置到新文件 InvoiceProductsExtended.cs 的部分类中
  2. 您还可以将 InvoiceProducts 替换为新类(包装器或继承器)并绑定到它
  3. 使用绑定转换器从原始属性计算新值

【讨论】:

  • 当我通过dataContext.saveChanges() 方法更新我的数据库时,这种方法是否会导致任何问题?
  • 新的计算属性未映射到 EF EDMX 文件(映射)。我认为 dataContext.saveChanges() 应该可以正常工作
  • 谢谢,我也去看看
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-16
  • 2015-01-06
相关资源
最近更新 更多