When an XAF list view has no selection-based actions available, the selection box still appears in the grid. Users get confused. In this post, we’ll look at a workaround.
The problem
In the XAF MainDemo, lets make Departments read-only for the User role.
Then start the web application, login as John and navigate to the Departments list view. There is a column selection box, but it serves no purpose. There are no actions that depend on a grid selection.
Without the SelectionColumnVisibilityController
The fix
Here is a controller which calculates whether there are any available actions which require one or more rows to be selected. If there are none, the selection box will not appear.
Add the following controller to the MainDemo.Module.Web project. It hides the selection box if there are no actions which depend on a grid selection.
usingSystem;usingDevExpress.ExpressApp;usingDevExpress.ExpressApp.Actions;usingDevExpress.ExpressApp.Editors;usingDevExpress.ExpressApp.SystemModule;usingDevExpress.Web;usingSystem.Linq;namespaceMainDemo.Module.Web.Controllers{publicclassSelectionColumnVisibilityController:ViewController{publicSelectionColumnVisibilityController(){TargetViewType=ViewType.ListView;}privateboolIsSelectionColumnVisible(){boolisSelectionColumnRequired=false;// remove checkbox if there are no available actionsforeach(ControllercontrollerinFrame.Controllers){if(!controller.Active)continue;if(controller.Actions.Count==0)continue;boolallowEdit=true;if((FrameisNestedFrame)&&(((NestedFrame)Frame).ViewItemisPropertyEditor))allowEdit=(bool)((PropertyEditor)((NestedFrame)Frame).ViewItem).AllowEdit;foreach(ActionBaseactionincontroller.Actions){if(action.SelectionDependencyType==SelectionDependencyType.RequireMultipleObjects){if(action.Active||IsActionInactiveBySelectionContext(action)){if(action.Enabled||IsActionDisabledBySelectionContext(action)){isSelectionColumnRequired=true;break;}}}}if(isSelectionColumnRequired)break;}returnisSelectionColumnRequired;}privateboolIsActionInactiveBySelectionContext(ActionBaseaction){if(action.Active)returntrue;else{foreach(stringiteminaction.Active.GetKeys()){if(item==ActionBase.RequireMultipleObjectsContext||item==ActionBase.RequireSingleObjectContext)continue;if(!action.Active[item])returnfalse;}returntrue;}}privateboolIsActionDisabledBySelectionContext(ActionBaseaction){if(action.Enabled)returntrue;else{foreach(stringiteminaction.Enabled.GetKeys()){if(item==ActionBase.RequireMultipleObjectsContext||item==ActionBase.RequireSingleObjectContext||item==ActionsCriteriaViewController.EnabledByCriteriaKey)continue;if(!action.Enabled[item])returnfalse;}returntrue;}}protectedoverridevoidOnViewControlsCreated(){base.OnViewControlsCreated();ASPxGridViewgrid=((ListView)this.View).Editor.ControlasASPxGridView;if(grid!=null){grid.Load+=grid_Load;grid.DataBound+=grid_DataBound;}}protectedoverridevoidOnDeactivated(){base.OnDeactivated();ASPxGridViewgrid=((ListView)this.View).Editor.ControlasASPxGridView;if(grid!=null){grid.DataBound-=grid_DataBound;grid.Load-=grid_Load;}}voidgrid_Load(objectsender,EventArgse){SetSelectionColumnVisibility(sender,e);}voidgrid_DataBound(objectsender,EventArgse){SetSelectionColumnVisibility(sender,e);}privatevoidSetSelectionColumnVisibility(objectsender,EventArgse){boolisSelectionColumnVisible=IsSelectionColumnVisible();if(!isSelectionColumnVisible){vargrid=(ASPxGridView)sender;varselectionBoxColumn=grid.Columns.OfType<GridViewCommandColumn>().Where(x=>x.ShowSelectCheckbox).FirstOrDefault();if(selectionBoxColumn!=null){selectionBoxColumn.Visible=false;}}}}}
Run the application again and see the difference. Now the grid looks like this. Notice, there is no longer a selection box on the row.
By the way, this is how it looks with old-style XAF web apps.