Search This Blog

Monday, December 14, 2015

How to hide or filter out certain types from the drop-down editor for the System.Type properties

I want to share my recent article on the subject that demonstrates yet another example of how flexible and powerful XAF really is. There, I will describe three possible ways of accomplishing the same task, starting with the most recommended platform-agnostic solution at the business model level (write once, work everywhere) and finishing by manipulating end control properties in the UI for certain platforms and contexts. 

So, let me quite myself from this KB article in our Support Center:

Type properties are defined in code and represented in the application UI for different platforms as described in the eXpressApp Framework > Concepts > Business Model Design > Data Types Supported by built-in Editors > Type Properties documentation article. Thus, there are three possible ways of accomplishing this task, which apply at different application layers:

1. Data model level
Since the System.Type properties are usually declared using System.ComponentModel.TypeConverterAttribute that provides a TypeConverter class for setting a list of default values or default choice of types shown in the editor, you can provide a custom TypeConverter that contains only required types. A custom TypeConverter class can either be implemented from scratch or inherited from built-in XAF ones like LocalizedClassInfoTypeConverter or ClassInfoTypeConverter.
If you own the source code of the target System.Type property, you can change its declaration directly in the code of your business class. Otherwise (e.g., you want to customize the behavior of a class in a third-party assembly), you can remove the default TypeConverterAttribute and replace it with a required one at runtime as per eXpressApp Framework > Concepts > Business Model Design > Types Info Subsystem > Customize Business Object's Metadata. The attached source code ( illustrates this approach in action for a scenario with filtering out a list of types shown for the TargetType editor of the security type permission record in the UI (SecuritySystemTypePermissionsObjectBase).

Note: this approach allows you to write platform-agnostic logic only once. Also, in certain scenarios (e.g., reporting and Pivot Chart analysis) it is possible to decorate a business type with VisibleInReportsAttribute or manage the IModelClassReportsVisibility.IsVisibleInReports property in the Model Editor for that purpose.

2. UI PropertyEditor level
Since the System.Type properties are represented by the TypePropertyEditor, VisibleInReportsTypePropertyEditor classes for WinForms and ASPxTypePropertyEditor, ASPxVisibleInReportsTypePropertyEditor classes for ASP.NET in the XAF framework to build the resultant application UI, you can create descendants of these classes and override their virtual IsSuitableType method, which represents a predicate called for each checked type item before adding it into the editor control.
Refer to the eXpressApp Framework > Concepts > UI Construction > View Items > Implement Custom Property Editors  article and see some pseudo-code at Q337620 for more implementation details.
Note: this approach allows you to write platform-agnostic logic only once and then call it twice from two descendants of the standard PropertyEditor class for each platform without knowing details of control implementation for each platform.

3. UI control level
Since the System.Type properties are represented by ImageComboBoxEdit for WinForms and ASPxComboBox for ASP.NET in the resultant application UI, you can access these controls and customize their items directly in code of your ViewControllers. Refer to the Access Editor Settings  article for more implementation details.
Note: this approach does not only require writing platform-dependent code. Despite its flexibility, it requires knowledge of each platform and their controls' low level implementation details as well as additional or different code for ListView/DetailView and other standard scenarios.

No comments:

Post a Comment