XAF And ReflectionMagic – Quick Trick


https://github.com/ReflectionMagic/ReflectionMagic

Let’s see an example…

The Problem:

Solutions Recommended by the DevExpress Team:

  1. Include the ID property in the display value using a calculated property:
    Make a Property Calculable
    How to: Specify a Display Member for a Lookup Editor, Detail Form Caption, and more
    However, in this case, IDs will be sorted as strings – i.e., 10 will be prior to 2.
  2. Implement a custom Property Editor that configures the underlying Razor component as required: How to: Implement a Property Editor Based on a Custom Component (Blazor). For example, you can use the DxComboBox component. Alternatively, use a button edit that shows a popup ListView as shown in the following ticket: Blazor – How to display LookupPropertyEditor’s items in a popup window with the New action.

Our Solution

The XAFDefaultProperty is used as the display members in lookups.

Let’s say we have a XAF Win and Blazor app.

In Windows we are sorting the lookup by a different property than the Default Property. (Of course that sorting is not working on Blazor)

What we did (Only on Blazor)

 public override void CustomizeTypesInfo(ITypesInfo typesInfo)
    {
        base.CustomizeTypesInfo(typesInfo);
        
        typesInfo.FindTypeInfo(typeof(LookupCountry)).FindAttribute<XafDefaultPropertyAttribute>().AsDynamic().name = nameof(LookupCountry.Sort);
    }

But then we have the issue that in Blazor the Display Member in the Lookups is now the Sort property.

Property Editor to the rescue

[PropertyEditor(typeof(LookupCountry), "LookupCountryLookupPropertyEditor", false)]
    public class LookupCountryLookupPropertyEditor : LookupPropertyEditor
    {

        public LookupCountryLookupPropertyEditor(Type objectType, IModelMemberViewItem model) : base(objectType, model)
        {
           

        }

        protected override void OnControlCreated()
        {
            base.OnControlCreated();
            if (Control is DxComboBoxAdapter adapter)
            {
                adapter.ComponentModel.TextFieldName = nameof(LookupCountry.COUNTRY);
                adapter.ComponentModel.ClearButtonDisplayMode = DevExpress.Blazor.DataEditorClearButtonDisplayMode.Never;
            }
        }
    }

Important line:

adapter.ComponentModel.TextFieldName = nameof(LookupCountry.COUNTRY);

That’s it. Crisis averted.

What do you guys think of this trick -we have a lot more of those 🙂

Lastly, don’t forget to add the reflection magic nuget packet to include the dark “magic” in your app.

Until next time. XAF out!

Leave a Reply

Your email address will not be published.