Simpler alternative for accessing master object from nested listview on XAF

629 views Asked by At

I want to know if there is simple and clear method other than this for accessing master object from a nested listview controller.

((PropertyCollectionSource)((ListView)View).CollectionSource).MasterObject

Do I have to write this in everywhere where i need to access master object?

I think this is not an elegant way and looks so lame.

2

There are 2 answers

2
ErikWitkowski On BEST ANSWER

Not tested yet, but you can use the following ViewController descendant:

public class NestedViewController : ViewController
{
    protected PropertyCollectionSource PropertyCollectionSource
    {
        get
        {
            return View is ListView ? ((ListView)View).CollectionSource is PropertyCollectionSource ? ((ListView)View).CollectionSource as PropertyCollectionSource : null : null;
        }
    }

    protected object MasterObject
    {
        get
        {
            return PropertyCollectionSource != null ? PropertyCollectionSource.MasterObject : null;
        }
    }
}
0
Kirsten On

Simplifying the above answer with C#6

public partial class NestedViewController : ViewController
{
    protected PropertyCollectionSource PropertyCollectionSource => (View as ListView)?.CollectionSource as PropertyCollectionSource;

    protected object MasterObject => PropertyCollectionSource?.MasterObject;
}

Also I moved it into a function

public static class HandyControllerFunctions
{
    public static object GetMasterObject(View view)
    {
        var propertyCollectionSource = (view as ListView)?.CollectionSource as PropertyCollectionSource;
        return propertyCollectionSource?.MasterObject ;
    }
}

and call it for example

var myObject = HandyControllerFunctions.GetMasterObject(View) as IMyObject