恢复从LINQ到SQL的更改后的WPF更新UI

问题描述 投票:0回答:1

我读了这个问题,但遗憾的是在恢复变更后没有找到更新ui的任何解决方案。

How can I reject all changes in a Linq to SQL's DataContext?

我的所有属性都来自INotifyPropertyChanged。

var changeSet = dataContext.GetChangeSet();
if (changeSet != null)
{
    var updates = changeSet.Updates.ToArray();
    dataContext.Refresh(RefreshMode.OverwriteCurrentValues, changeSet.Updates);

    foreach (var o in updates)
    {
        INotifyPropertyChanged entity = (INotifyPropertyChanged)o;
        // What i have to do to force them trigger PropertyChanged event ???
    }
}
c# wpf linq datacontext inotifypropertychanged
1个回答
1
投票

首先,我为所有Linq2SQL类添加一个方法:

public partial class Setting
{
    public void SendPropertyChangedFromOutside(string propName)
    {
        SendPropertyChanged(propName);
    }
}

public partial class User
{
    public void SendPropertyChangedFromOutside(string propName)
    {
        SendPropertyChanged(propName);
    }
}

然后调用UndoDBChanges方法:

public static void UndoDBChanges(System.Data.Linq.DataContext dataContext) {
var changeSet = dataContext.GetChangeSet();
if (changeSet != null)
{
    var updates = changeSet.Updates.ToArray();
    dataContext.Refresh(RefreshMode.OverwriteCurrentValues, changeSet.Updates);

    foreach (var o in updates)
    {
        try
        {
            Type t = o.GetType();
            dynamic obj = Convert.ChangeType(o, t);

            foreach (System.Reflection.PropertyInfo prop in t.GetProperties())
            {
                obj.SendPropertyChangedFromOutside(prop.Name);
            }
        }
        catch
        { }
    }
}}
© www.soinside.com 2019 - 2024. All rights reserved.