using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Collections.ObjectModel; namespace HeuristicLab.OKB.Cockpit.Admin { public static class EntityEditorSupport { public static IEnumerable GetObjects(DataTable table, IEnumerable propertyNames) where T : class { foreach (DataRow row in table.Rows) { if (row["Id"] != DBNull.Value) { T o = Activator.CreateInstance(); foreach (string name in propertyNames) typeof(T).GetProperty(name).SetValue(o, row[name] == DBNull.Value ? null : (object)row[name], null); yield return o; } } } public static void ShowSelector(Func filter, Action> updater) where T : class { var loader = new TableLoadWorker(typeof(T).Name, false); loader.RunWorkerCompleted += (s, a) => { var objects = GetObjects(loader.Table, new[] { "Id", "Name", "Description", }).ToList(); RelationEditorWindow relationEditor = new RelationEditorWindow() { Title = typeof(T).Name, AvailableObjects = new ObservableCollection(objects.Cast()), SelectedObjects = new ObservableCollection(objects.Where(o => filter(o)).Cast()), }; if (relationEditor.ShowDialog() == true) { updater(relationEditor.SelectedObjects.Cast()); } }; loader.RunWorkerAsync(); } } }