Skip to main content

Hide the Protected Content Columns in a List View and Property Editors in a Detail View

  • 2 minutes to read

This topic describes how to use the Conditional Appearance Module in order to hide List View columns and Detail View Property Editors that are prohibited by the Security System and display the ‘*******‘ placeholders.

  • Add the Conditional Appearance Module to the platform-agnostic module project (if it was not added before). To do this, open the MySolution.Module\Module.cs (Module.vb) file and add the following code to the Module constructor:

    using DevExpress.ExpressApp.ConditionalAppearance;
    // ...
    namespace MySolution.Module {
        public sealed partial class MySolutionModule : ModuleBase {
            public MySolutionModule() {
                // ...
                RequiredModuleTypes.Add(typeof(ConditionalAppearanceModule));
            }
        }
    }
    

    In .NET Framework projects, you can alternatively invoke the Module Designer or Application Designer, and drag the ConditionalAppearanceModule item from the Toolbox to the Modules panel. Make sure to rebuild your solution after making changes in the Designer.

  • Implement the following Controller class in the platform-agnostic module project.

    using DevExpress.ExpressApp;
    using DevExpress.ExpressApp.Editors;
    using DevExpress.ExpressApp.ConditionalAppearance;
    using DevExpress.ExpressApp.Security;
    // ...
    public class HideProtectedContentController : ViewController<ObjectView> {
        private AppearanceController appearanceController;
        protected override void OnActivated() {
            base.OnActivated();
            appearanceController = Frame.GetController<AppearanceController>();
            if(appearanceController != null) {
                appearanceController.CustomApplyAppearance += appearanceController_CustomApplyAppearance;
            }
        }
        protected override void OnDeactivated() {
            if(appearanceController != null) {
                appearanceController.CustomApplyAppearance -= appearanceController_CustomApplyAppearance;
            }
            base.OnDeactivated();
        }
        void appearanceController_CustomApplyAppearance(object sender, ApplyAppearanceEventArgs e) {
            if(e.AppearanceObject.Visibility == null || e.AppearanceObject.Visibility == ViewItemVisibility.Show) {
                SecurityStrategy security = Application.GetSecurityStrategy();
                if(View is ListView) {
                    if(e.Item is ColumnWrapper) {
                        if(!security.CanRead(View.ObjectTypeInfo.Type,
                            ((ColumnWrapper)e.Item).PropertyName)) {
                            e.AppearanceObject.Visibility = ViewItemVisibility.Hide;
                        }
                    }
                }
                if(View is DetailView) {
                    if(e.Item is PropertyEditor) {
                        object targetObject = e.ContextObjects.Length > 0 ? e.ContextObjects[0] : null;
                        if(targetObject != null && !security.CanRead(View.ObjectTypeInfo.Type, 
                            View.ObjectSpace, 
                            View.ObjectSpace.GetKeyValue(targetObject), 
                            ((PropertyEditor)e.Item).PropertyName)) {
                                e.AppearanceObject.Visibility = ViewItemVisibility.Hide;
                            }
                    }
                }
            }
        }
    }
    

Note

  • HideProtectedContentController may have a negative impact on the application performance in complex scenarios.
  • Hidden states of list view columns are persisted in user model differences. So, if you will eventually grant access to a certain property, the view model needs to be manually adjusted or reset.