我正在使用objectlistview (olv) 来显示依赖于主记录的子属性集。这是使用 linq 的 ac# 应用程序。我不使用 olv 作为行编辑器;相反,我将底层子记录弹出到一个动态的、更详细的非 wpf 对话框中。如果 olv 的列的模型方面依赖于对象(即外键),则 olv 显示不会刷新。
例如,
olv 显示包含状态日志,其中一列是“Status.Code”,它与对象“Log”相关,如下所示:“Log.Status”。因为是linq,所以“Log.StatusId”也存在于linq DataContext中(但未配置为显示在olv中)。“Log.StatusId”从编辑对话框正确返回,并且“Log.Status”在编辑对话框终止后立即正确填充。此外,linq 可以正确保存编辑。
我已经尝试过使用 olv 的 Invalidate() 和 BuildList() 以及几天的不祥之物,但都失败了。这是一个普通的 olv - 不是 rapidlistview 或 datalistview。欢迎任何观点。
下面的代码强调了外键的处理。对于非 olv 用户,olv 的配置与大多数其他 Windows 窗体控件一样。
...
object old = DataService.Clone<object>(olv.SelectedObject);
// where old ~ reference base for changes to olv object - for state management and linq
object row = olv.SelectedObject;
// where row ~ object that receives edits and undergoes updates via linq
Dictionary<string, object> rowState = new Dictionary<string, object>();
// where ~ rowState<fieldName,originalValue>
RecordDetail dlg = new RecordDetail(GetUser(), master.GetType(), row, rowState);
// where ~ GetUser() & master.GetType() configure form RecordDetail
DialogResult dr = dlg.ShowDialog();
if (dr == DialogResult.OK)
{
if (row != null && rowState != null && rowState.Count > 0)
{
int id = DataService.GetPrimaryKeyValue(row);
if (id > 0) /// if not new
{
int pk = DataService.GetPrimaryKeyValue(old);
MultiState state = getChildState(olv); // olv.Tag contains state
foreach (KeyValuePair<string, object> change in rowState)
{
MethodInfo mi = old.GetType().GetMethod(DataService.LINQ_GET + change.Key);
object newValue = mi.Invoke(row, null);
bool ok = DataService.ManageMultiStateUpdate(ref state, pk, change.Key, newValue, change.Value, old);
/// INFO populate fk objects for olv // <== works ok
Type tdomain = DataService.GetForeignKeyTypeAsAliasSafe(old.GetType(), change.Key);
if (tdomain != null)
{
object fko = GetForeignKey(tdomain, change.Value);
mi = row.GetType().GetMethod(DataService.LINQ_SET + change.Key.Replace(DataService.LI_ID, ""));
object[] args = { fko };
mi.Invoke(row, args);
}
...
}
olv.BuildList(); // <== both this and olv.Invalidate() fail to display foreign key updates
...
}
...
}
...
}
...