Ah, a new day and a new approach now that I have a work around for the last bug.
Lets make that nice and explicit.
Here is my new controller to work on any object that implements IHCategory and IObjectSpaceLink
public partial class ICategoryController : ViewController
{
private NewObjectViewController controller;
public ICategoryController()
{
InitializeComponent();
TargetObjectType = typeof(IHCategory);
}
protected override void OnActivated()
{
controller = Frame.GetController<NewObjectViewController>();
controller.ObjectCreated += controller_ObjectCreated;
base.OnActivated();
}
private void controller_ObjectCreated(object sender, ObjectCreatedEventArgs e)
{
SetParent(e, View);
}
private static void SetParent(ObjectCreatedEventArgs e, View view)
{
var createdObject = e.CreatedObject as IHCategory;
WorkAroundBug(createdObject);
var propertyCollectionSource = (view as ListView)?.CollectionSource as PropertyCollectionSource;
if (!(propertyCollectionSource?.MasterObject is IHCategory master)) return;
var m = e.ObjectSpace.GetObject(master);
createdObject.Parent = m;
m.Children.Add(createdObject);
}
private static void WorkAroundBug(IHCategory createdObject)
{
// https://www.devexpress.com/Support/Center/Question/Details/T704563/xaf-entity-framework-self-referencing-table-example-with-aggregated-attribute-for
var c2 = createdObject as IObjectSpaceLink;
// Do not comment out. This is a bug work around
var numModifiedObjects = c2.ObjectSpace.ModifiedObjects.Count;
}
protected override void OnDeactivated()
{
controller.ObjectCreated += controller_ObjectCreated;
base.OnDeactivated();
}
}