如何撤消对自我跟踪实体所做的所有更改?

发布于 2024-11-18 22:50:10 字数 220 浏览 10 评论 0原文

我有一个客户端应用程序,可以通过 WCF 下载许多 STE。

使用 WPF 应用程序,用户可以从列表框中选择一个实体,并通过弹出的用户控件对其进行编辑。由于 UserControl 直接绑定到对象,因此当用户进行更改时,它当然会影响该对象。

我想提供一个取消功能,它将撤消对实体所做的所有更改。

有什么想法吗?

I have a client application that downloads a number of STE's via WCF.

Using a WPF application, users can select an entity from a ListBox, and edit it via a popup UserControl. As the UserControl is bound directly to the object, when a user makes a change it of course affects the object.

I would like to provide a Cancel function which will undo all changes made to the entity.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人间☆小暴躁 2024-11-25 22:50:10

您可以保留该实体的原始副本。并编辑它的克隆版本。
如果用户取消更改,您只需继续使用原始副本即可。

You can keep a original copy of the entity. And edit a cloned version of it.
If the user cancels the changes you simply keep using the original copy.

欢你一世 2024-11-25 22:50:10

我想说的是,当您在绑定的 PropertyChanged 事件中使用 WPF 时,会保存一个带有键 PropertyName 和值 PropertyValue 的字典。并在使用反射恢复状态后

I would say as you use WPF just in binded PropertyChanged event save a Dictionary with key PropertyName and value PropertyValue. And after restore the state by using reflection

他夏了夏天 2024-11-25 22:50:10

到目前为止我正在使用这个解决方案
扩展方法

using System.Collections.Generic;
using System.Reflection;

namespace WpfApplication4
{
    public static class EFExtensions
    {
        /// <summary>
        /// Rejects changes made by user
        /// </summary>
        /// <param name="param">Object implementing IObjectWithChangeTracker interface</param>
        public static void RejectChanges(this IObjectWithChangeTracker param)
        {
            OriginalValuesDictionary ovd = param.ChangeTracker.OriginalValues;
            PropertyInfo[] propertyInfos = (param.GetType()).GetProperties();

            foreach (KeyValuePair<string, object> pair in ovd)
            {
                foreach (PropertyInfo property in propertyInfos)
                {
                    if (property.Name == pair.Key && property.CanWrite)
                    {
                        property.SetValue(param, pair.Value, null);
                    }
                }
            }
        }
    }
}

主要代码

using System.Linq;

namespace WpfApplication4
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            using (var db = new PlatEntities())
            {
                PacketEPD pd = (from epd in db.PacketEPD
                                select epd).First();
                pd.ChangeTracker.ChangeTrackingEnabled = true;
                pd.EDNo = "1";
                pd.RejectChanges();
            }
        }
    }
}

I'm using this solution so far
Extension method

using System.Collections.Generic;
using System.Reflection;

namespace WpfApplication4
{
    public static class EFExtensions
    {
        /// <summary>
        /// Rejects changes made by user
        /// </summary>
        /// <param name="param">Object implementing IObjectWithChangeTracker interface</param>
        public static void RejectChanges(this IObjectWithChangeTracker param)
        {
            OriginalValuesDictionary ovd = param.ChangeTracker.OriginalValues;
            PropertyInfo[] propertyInfos = (param.GetType()).GetProperties();

            foreach (KeyValuePair<string, object> pair in ovd)
            {
                foreach (PropertyInfo property in propertyInfos)
                {
                    if (property.Name == pair.Key && property.CanWrite)
                    {
                        property.SetValue(param, pair.Value, null);
                    }
                }
            }
        }
    }
}

Main code

using System.Linq;

namespace WpfApplication4
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            using (var db = new PlatEntities())
            {
                PacketEPD pd = (from epd in db.PacketEPD
                                select epd).First();
                pd.ChangeTracker.ChangeTrackingEnabled = true;
                pd.EDNo = "1";
                pd.RejectChanges();
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文