我读了这个问题,但不幸的是,在还原更改后没有找到任何更新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 ???
}
}
答案 0 :(得分: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
{ }
}
}}