C# 将更改从LINQ恢复为SQL后WPF更新UI
我读了这个问题,但不幸的是,在恢复更改后,没有找到任何更新ui的解决方案 我的所有属性都派生自INotifyPropertyChangedC# 将更改从LINQ恢复为SQL后WPF更新UI,c#,wpf,linq,datacontext,inotifypropertychanged,C#,Wpf,Linq,Datacontext,Inotifypropertychanged,我读了这个问题,但不幸的是,在恢复更改后,没有找到任何更新ui的解决方案 我的所有属性都派生自INotifyPropertyChanged var changeSet = dataContext.GetChangeSet(); if (changeSet != null) { var updates = changeSet.Updates.ToArray(); dataContext.Refresh(RefreshMode.OverwriteCurrentValues, cha
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 ???
}
}
首先,我向所有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
{ }
}
}}
这完全取决于你的设计。通常,您需要检查每个UI对象,看看它们是否需要更新。@Bijan感谢您的回复,但我正在寻找其他解决方案。问题的根源是dataContext.Refresh()方法,它不会触发onPropertyChanged事件。你对此有什么建议吗?我不明白你的问题。为什么要在事件上触发刷新?断点是否达到刷新?你在刷新之前保存了上下文吗?@Bijan让我再解释一下。我有一个带有一些用户控件的WPF应用程序。所有UI元素都绑定到数据上下文,对绑定控件的任何更改都会自动反映到数据上下文的变更集。如果我在UI中更改文本框内容,将触发onPropertyChanged事件。但是如果我调用DataContext.Refresh(RefreshMode.OverwriteCurrentValues,changeSet.Updates);什么都没有发生。因此,我正在寻找一种手动执行相同行为的方法。在我看来,如果您需要,设计是有缺陷的。您应该使用视图模型,并且仅在用户同意后修改实体对象。如果它们取消编辑操作,您只需放弃已更改的视图模型对象,并且任何图元对象都未看到更改。