View.CustomizeViewShortcut Event
Occurs when the View.CreateShortcut method creates a View Shortcut for the current View.
Namespace: DevExpress.ExpressApp
Assembly: DevExpress.ExpressApp.v24.2.dll
NuGet Package: DevExpress.ExpressApp
Declaration
Event Data
The CustomizeViewShortcut event's data class is CustomizeViewShortcutArgs. The following properties provide information specific to this event:
Property | Description |
---|---|
ViewShortcut | Specifies the created View Shortcut. |
Remarks
Handle this event to add custom key/value pairs to View Shortcuts when they are created. Note that you should also add custom keys to the ViewShortcut.EqualsDefaultIgnoredParameters list, to ensure that they do not break built-in functionalities.
Important
Do not modify values corresponding to keys that already exist in newly created View Shortcuts. XAF uses this information internally, and its modification may cause issues with built-in functionalities.
Follow the steps below to handle the CustomizeViewShortcut
event to add a filter expression to a List View Shortcut.
- Use the WebViewShortcutHelper.RegisterParameterName method to register a custom shortcut key (Only for ASP.NET WebForms applications).
- Navigate to the MySolution.Module\Module.cs file. In the
Setup
method , subscribe to the CustomProcessShortcut event. - In this event handler, create a new List View that contains filtered objects and subscribe to the
CustomizeViewShortcut
event. In the event handler, add the List View’s filter expression with a custom key to the Shortcut.
using DevExpress.Data.Filtering; using DevExpress.ExpressApp.Model; using DevExpress.ExpressApp.Web; // ... public sealed partial class MainDemoModule : ModuleBase { public const string CustomFilterKey = "CustomFilter"; public MainDemoWebModule() { InitializeComponent(); WebViewShortcutHelper.RegisterParameterName(CustomFilterKey); } public override void Setup(XafApplication application) { base.Setup(application); application.CustomProcessShortcut += Application_CustomProcessShortcut; } void Application_CustomProcessShortcut(object sender, CustomProcessShortcutEventArgs e) { XafApplication application = (XafApplication)sender; if (e.Shortcut.ContainsKey(CustomFilterKey)) { IModelListView listViewModel = application.FindModelView(e.Shortcut.ViewId) as IModelListView; if (listViewModel != null) { CriteriaOperator filter = CriteriaOperator.Parse(e.Shortcut[CustomFilterKey]); e.View = CreateFilteredView(application, listViewModel.ModelClass.TypeInfo.Type, listViewModel.Id, filter); e.Handled = true; } } } public static ListView CreateFilteredView(XafApplication application, Type objectType, string viewId, CriteriaOperator filter) { IObjectSpace objectSpace = application.CreateObjectSpace(objectType); CollectionSourceBase source = application.CreateCollectionSource(objectSpace, objectType, viewId); ListView listView = application.CreateListView(viewId, source, true); listView.CollectionSource.Criteria[CustomFilterKey] = filter; listView.CustomizeViewShortcut += ListView_CustomizeViewShortcut); return listView; } static void ListView_CustomizeViewShortcut(object sender, CustomizeViewShortcutArgs e) { e.ViewShortcut[CustomFilterKey] = ((ListView)sender).CollectionSource.Criteria[CustomFilterKey].ToString(); } //... }
In the YourSolutionName\Module\Controllers folder, create a View Controller that displays the filtered View.
using DevExpress.Data.Filtering; using DevExpress.ExpressApp; using DevExpress.ExpressApp.Actions; // ... public class MyContactsController : ObjectViewController<ObjectView, Contact> { public MyContactsController() { SimpleAction action = new SimpleAction(this, "ShowManagedContacts", DevExpress.Persistent.Base.PredefinedCategory.View); action.SelectionDependencyType = SelectionDependencyType.RequireSingleObject; action.Execute += new SimpleActionExecuteEventHandler(action_Execute); } void action_Execute(object sender, SimpleActionExecuteEventArgs e) { Contact currentObject = (Contact)e.SelectedObjects[0]; CriteriaOperator filter = CriteriaOperator.Parse("Manager.Oid = ?", currentObject.Oid); string viewId = Application.GetListViewId(typeof(Contact)); ListView listView = MainDemoModule.CreateFilteredView(Application, typeof(Contact), viewId, filter); e.ShowViewParameters.CreatedView = listView; } }
This code changes the default behavior of WinForms and ASP.NET Web Forms applications as follows:
- A WinForms application with the MDI UI type opens List Views with the same ID and different filters in different tabs. (Default behavior: an application activates an existing tab if its List View has the same ID as a filtered View.)
- An ASP.NET Web Forms application restores a List View’s filter that is set in the
SimpleAction.Execute
handler. This occurs when a user clicks a browser’s Back button to navigate back to a previously opened List View. (Default behavior: an application resets the filter when a user navigates back from a filtered List View.)
Note
If a filter expression contains special characters (blanks or punctuation marks) it can be misinterpreted in an HTTP stream. To avoid this, use the HttpUtility.UrlEncode method to write a criteria string to a ViewShortcut and the HttpUtility.UrlDecode method to get a CriteriaOperator from this ViewShortcut.