【问题标题】:Access Fields in a C# CollectionViewSource访问 C# CollectionViewSource 中的字段
【发布时间】:2018-03-13 09:47:23
【问题描述】:

我已使用 EF6 访问数据库并从名为 Asset 的表中检索 DbSet 实体 (DbSet<Asset> Assets)。

表的行存储在为 WPF Xaml GUI 提供 DataContext 的 CollectionViewSource (CollectionViewSource x:Key="assetViewSource") 中。

GUI 的字段代表表格的列,它们绑定到assetViewSource 中的字段,如下所示:

Text="{Binding Path=AssetCategory, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"

一切正常,用户可以通过 GUI 与数据库交互。

谁能告诉我如何从后面的代码中访问 assetViewSource 集合中的字段中的数据,这些数据已从 GUI 更新后但在更改保存到 RMS_DB 数据库之前?

我希望能够在后面的代码中做一些事情,如下所示:

codebehindField = assetViewSource.Asset.AssetCategory.

【问题讨论】:

    标签: c# .net wpf entity-framework


    【解决方案1】:

    我在题为“实体框架使用属性值”的 MSDN 文档的“获取和设置单个属性的当前值或原始值”部分中找到了回答此问题所需的信息,地址为 https://msdn.microsoft.com/en-us/library/jj592677(v=vs.113).aspx 我将在下面发布我为解决我的问题而编写的代码,以防其他人发现它有用....

        // Update "DateRecordLastUpdated" in GUI because changes have been made to the Process' fields.
        // Get the entity's primary key from the GUI and use it to Find the corresponding entity in the CollectionView.
        int OrgUnitID = int.Parse(viewAsset_assetOrgUnitIDTextBox.Text);
        int AssetClassID = int.Parse(viewAsset_assetAssetClassIDTextBox.Text);
        int AssetID = int.Parse(viewAsset_assetIDTextBox.Text);
        var selectedEntity = rmsDb.Assets.Find(OrgUnitID, AssetClassID, AssetID);       // Primary key components must be in sequence.
        rmsDb.Entry(selectedEntity).Property(u => u.DateRecordLastUpdated).CurrentValue = DateTime.UtcNow;
        BindingOperations.GetBindingExpression(viewAsset_dateRecordLastUpdated, TextBox.TextProperty).UpdateTarget();           //Trigger GUI binding refresh
        rmsDb.SaveChanges(); 
    

    非常感谢那些对我之前的帖子发表评论的人。

    【讨论】:

      猜你喜欢
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多