本文介绍了WPF克隆/分离对象编辑的问题 - 的标准是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个ObservableCollection包装一些自己生成的实体框架对象。
当用户希望编辑一些值,我打开包含字段的弹出窗口,当用户改变和preSS保存 - 将更改保存到数据库中,并且绑定控件的变化,因为它是一个ObservableCollection

I am using an ObservableCollection to wrap some of my generated entity framework objects.When the user wants to edit some values , i am opening a popup windows that contains fields, and when the user changes and press save - the changes are saved to the database, and the binded controls are changes as it is an observablecollection.

要prevent来自同一个绑定对象上工作(它会导致同一时间每个绑定控件的视觉变化),我想使用一些功能克隆的对象,然后拆下原来的用户,连接克隆对象,并且将其保存到数据库中。问题是,克隆对象不正确地保存到数据库中。如果我只尝试分离的对象,编辑,然后再连接 - 分离,当它失去了所有的父母和孩子refernces ...

To prevent the user from working on the same binded object (it causes the visual change of every binded control on the same time) i want to use some functionality the clone the object, and then detach the original, attach the cloned object, and save it to the database. The problem is that the cloned object does not save properly to the database. If i try only to detach the object, edit and then attach - when detached it loses all its parent and child refernces...

什么是WPF中的CRUD的标准呢?我怎样才能干净编辑当前行,同时保持在一个ObservableCollection?

What is the CRUD standard in WPF? How can i cleanly edit a current row, while keeping it in an ObservableCollection?

请帮助....

奥兰

推荐答案

好吧,似乎我已经找到了细解。

Well it seems that i have found a fine solution.


  1. 首先实现你可复制的对象容器:

  1. First implement your cloneable object container :

public class ClonableObjectContainer : Object , ICloneable
{
    private Object data;


public ClonableObjectContainer(Object obj)
{
    data = obj;
}


public Object Data
{
    get { return data; }
}


public object Clone()
{
    return (ClonableObjectContainer)this.MemberwiseClone();
}

}


  • 然后使用这个对象,其克隆方法来建立独立编辑的对象:

  • Then use this object with its Clone method to create your detached edit object:

             ClonableObjectContainer coc = new ClonableObjectContainer(actor);
             EntityObject editEntity = (EntityObject)coc.Data;
    


  • 进行更改后,分离从ObjectContext的原始对象
    ,装上克隆的对象,改变其状态为 EntityState.Modified 优雅地保存:

            ContextManager.CurrentObjectContext.Detach(oldItem);
            ContextManager.CurrentObjectContext.Attach((IEntityWithKey)item);
            ContextManager.CurrentObjectContext.ObjectStateManager.ChangeObjectState(item, EntityState.Modified);
            ContextManager.Save();
    


  • 希望它可以帮助...
    奥兰

    Hope it helps...Oran

    编辑:如果以下事项不为你工作,请检查继续讨论:http://stackoverflow.com/questions/4034477/entity-framework-attach-exception-after-clone

    EDIT : If the followings does not work for you, please check the continued discussion : http://stackoverflow.com/questions/4034477/entity-framework-attach-exception-after-clone

    这篇关于WPF克隆/分离对象编辑的问题 - 的标准是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-23 17:07