Devexpress xaf ungroup layout of inherited class.

2019-06-05 08:24发布

问题:

I am using devexpress xaf to create a multi platform app. I have class 'commonFields' which contains common fields "creation_date, created_by" etc. all other classes inherit from this class. My problem is that, in detailView layout, common fields are displayed in a group labled commonFields.

I know it can be solved using the model editor where I can simply right click over the group and choose ungroup, but if I have lets say more than 50 tables I will have to edit all the layouts individually from the GUI.

I wanted to know if there is any other way, a method or some attribute I can add to the class which will display inherited fields in the same group.

Thankyou

回答1:

Sure you can do it via code. Here is the DevExpress documentation: Extend and Customize the Application Model in Code.

You need to provide your own ModelNodesGeneratorUpdater for the detail views part of the XAF model. It should look something like this:

public class MyDetailViewGeneratorUpdater : ModelNodesGeneratorUpdater<ModelDetailViewLayoutNodesGenerator> 
{
    public override void UpdateNode(ModelNode node) 
    {
        IModelDetailViewLayout layout = node as IModelDetailViewLayout;
        IModelDetailView detailView = (IModelDetailView)layout.Parent;
        if (!XafTypesInfo.Instance.FindTypeInfo(typeof(MyBase)).IsAssignableFrom(detailView.ModelClass.TypeInfo))   return;
        foreach (IModelDetailViewLayoutElement element in layout)
            UpdateLayoutItems(element, detailView.Items, XafTypesInfo.Instance.FindTypeInfo(typeof(MyBase)).FindMember("Description"));
    }

    private void UpdateLayoutItems(IModelDetailViewLayoutElement element, IModelDetailViewItems items, IMemberInfo member) {
        IModelLayoutItem item = element as IModelLayoutItem;
        IModelLayoutGroup group = element as IModelLayoutGroup;
        if(group != null){
            foreach(IModelDetailViewLayoutElement element1 in group)
            UpdateLayoutItems(element1, items, member);
        }
        else if (item != null) {
            RemoveFromGroup(item); // you just need to code this bit of magic
        }
    }
}

Don't forget to register your updater in the module:

public override void AddGeneratorUpdaters(ModelNodesGeneratorUpdaters updaters)
{
    base.AddGeneratorUpdaters(updaters);
    updaters.Add(new MyDetailViewGeneratorUpdater());
}


回答2:

I'm afraid there are no built-in attributes for this, but here are some directions:

First, be sure to read oficial DevExpress recomendations about Layout Customization.

Second, check Xpand framework's partial view inheritance. Surely a powerful tool, but will not necessarly save you time in the case you described above, but since I don't know all the complexity you've got, here's a player you can count in.

Also, have you considered giving the grouped layout a chance? You can find ways to make it attractive and useful. Check how to make it expandable: https://www.devexpress.com/Support/Center/Question/Details/Q101774

In case none of the solutions above meets your needs and all you need is to save time ungrouping it in all views, keep in mind you can edit xaf's model XML file directly. LayoutGroups generated after the parent object properties have the same ID across all views, and you can smartly set ShowCaption to false in those groups for all views ;).