【问题标题】:DataGrid´s SelectedItem lost Binding after UnitOfWork.SaveChanges在 UnitOfWork.SaveChanges 之后 DataGrid 的 SelectedItem 失去了绑定
【发布时间】:2014-04-10 13:15:53
【问题描述】:

我目前正在开发一个使用 EntityFramework 的应用程序。在这个应用程序中,有一个 DataGrid,SelectedItem 绑定到 ViewModel 上的一个属性:

<DataGrid AutoGenerateColumns="False"
          CanUserDeleteRows="True"
          IsReadOnly="True"
          IsSynchronizedWithCurrentItem="False"
          ItemsSource="{Binding ZuvTextCollection}"
          SelectedItem="{Binding SelectedEntity,
                                 UpdateSourceTrigger=PropertyChanged,
                                 Mode=TwoWay}">

现在我遇到的问题是,一旦存储数据,与 SelectedItem 的绑定似乎几乎丢失了。

编辑:我忘记了一个非常重要的点!对不起!此错误仅在创建新记录后发生。

protected override void OnSave()
{
    if (!this.OnCanSave())
    {
        return;
    }

    this.UnitOfWork.SaveChanges();
    this.IsDirty = this.UnitOfWork.ChangeTracker.HasChanges;
}

调用 this.UnitOfWork.SaveChanges 后,绑定到 ViewModel 中的 Property 不再存在。 DataGrid 中的行确实可以选择,但是Selection 的变化并没有到达ViewModel。

可能是什么?

PS:我已阅读以下帖子http://www.devexpress.com/Support/Center/Question/Details/Q509665,但我必须承认,这让我不知道如何解决我的问题。 感谢您的帮助!

【问题讨论】:

  • 显示SelectedEntity 的代码。在OnSave 之前/之后如何设置?同时展示SelectedEntity的实际实现

标签: c# wpf entity-framework mvvm datagrid


【解决方案1】:

@llll:我忘记了一个非常重要的点!对不起!并且此错误仅在创建新记录后发生。

public ZuvText SelectedEntity
{
    get
    {
        return this.selectedEntity;
    }

    set
    {
        var entity = value;

        if (this.selectedEntity != null)
        {
            this.selectedEntity.PropertyChanged -= this.OnEntityPropertyChanged;
            this.selectedEntity.DisableContinuousValidation();
        }

        if (entity != null)
        {
            entity.PropertyChanged += this.OnEntityPropertyChanged;
            entity.EnableContinuousValidation();
        }

        this.SetProperty(ref this.selectedEntity, entity);
    }
}

实体是在保存之前设置的,即在创建新项目时。 (当光标移动到新实体上时,即为 SelectedEntity)

现在,一位队友找到了原因。在我们实体的基类中 Equals 和 GetHashCode 被覆盖:

public bool Equals(TEntity other)
{
    if (object.ReferenceEquals(null, other))
    {
        return false;
    }

    if (object.ReferenceEquals(this, other))
    {
        return true;
    }

    return this.id == other.Id;
}

public override bool Equals(object obj)
{
    if (object.ReferenceEquals(null, obj))
    {
        return false;
    }

    if (object.ReferenceEquals(this, obj))
    {
        return true;
    }

    if (obj.GetType() != this.GetType())
    {
        return false;
    }

    return this.Equals(obj as TEntity);
}

public override int GetHashCode()
{
    // ReSharper disable once NonReadonlyFieldInGetHashCode
    return this.id.GetHashCode();
}

GetHashCode 的覆盖导致 id 在存储之前和之后的每个提供不同的 hashCode 并且不再检测到对象。 现在我们已经注释掉了 GetHashCode 的覆盖。但是,我不知道这个问题的真正解决方案。我的队友已经在寻找解决方案,但也许您有想法?

【讨论】:

    【解决方案2】:

    我们有解决方案! DataGrid Selector 找不到选中的对象,更新ID 后。这就是我们现在将实际的 EntityCollection 放入适配器或包装器类的原因。

    public class ZuvTextAdapter
    {
        public ZuvText ZuvText
        {
            get;
            set;
        }
    }
    

    DataSorce 看起来像这样:

    public ObservableCollection<ZuvTextAdapter> ZuvTextAdapterCollection...
    

    只是选择的实体为:

    public ZuvTextAdapter SelectedAdapter
    

    因此,我们的 GetHeshCode 解决了问题,并且在保存后绑定工作正常。 (由我的队友解决!)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 2012-06-12
      • 2014-10-10
      • 2013-01-26
      • 1970-01-01
      相关资源
      最近更新 更多