This example creates a pop-up window that allows users to perform a custom object search.
Implementation Details
- Create a non-persistent class with properties used to search persistent objects.
File to review: MySearchClass.csC#[DomainComponent] public class MySearchClass : NonPersistentBaseObject { [XafDisplayName("FirstName contains:")] public string FirstName { get; set; } [XafDisplayName("Age is equal to:")] public int Age { get; set; } // ... }
- Add a collection of persistent objects that contains the search results.
File to review: MySearchClass.csC#[DomainComponent] public class MySearchClass : NonPersistentBaseObject { // ... private IList<Contact> _contacts = new List<Contact>(); [XafDisplayName("Results:")] public IList<Contact> Contacts { get { return _contacts; } } }
- Add the MySearch action that populates the collection.
File to review: MySearchController.csC#public class MySearchController : ObjectViewController<DetailView, MySearchClass> { public MySearchController() { var myAction1 = new SimpleAction(this, "MySearch", "MySearchCategory"); myAction1.Execute += MyAction1_Execute; } // ... }
- When a user clicks the MySearch action, create a criterion based on the properties described in the first step and get persistent objects that fit this criterion.
File to review: MySearchController.csC#public class MySearchController : ObjectViewController<DetailView, MySearchClass> { // ... private void MyAction1_Execute(object sender, SimpleActionExecuteEventArgs e) { var mySearchObject = (MySearchClass)View.CurrentObject; var persistentOS = Application.CreateObjectSpace(typeof(Contact)); var criterion = CriteriaOperator.FromLambda<Contact>(x => x.FirstName.Contains(mySearchObject.FirstName) || x.Age == mySearchObject.Age); var results = persistentOS.GetObjects<Contact>(criterion); mySearchObject.SetContacts(results); } }
- Create the MyShowSearchAction to display the MySearchClass detail view from the Contact list view in a pop-up window.
File to review: MyShowSearchController.csC#public class MyShowSearchController : ObjectViewController<ListView, Contact> { public MyShowSearchController() { var mypopAction1 = new PopupWindowShowAction(this, "MyShowSearchAction", PredefinedCategory.Edit); mypopAction1.TargetViewNesting = Nesting.Root; mypopAction1.CustomizePopupWindowParams += MyAction1_CustomizePopupWindowParams; } private void MyAction1_CustomizePopupWindowParams(object sender, CustomizePopupWindowParamsEventArgs e) { var nonPersistentOS = (NonPersistentObjectSpace)Application.CreateObjectSpace(typeof(MySearchClass)); var persistentOS = Application.CreateObjectSpace(typeof(Contact)); nonPersistentOS.AdditionalObjectSpaces.Add(persistentOS); var obj = nonPersistentOS.CreateObject<MySearchClass>(); nonPersistentOS.CommitChanges(); var view = Application.CreateDetailView(nonPersistentOS, obj); e.View = view; } }
Files to Review
Documentation
- Non-Persistent classes
- How to: Show Persistent Objects in a Non-Persistent Object's View
- How to: Include an Action to a Detail View Layout
- Create, Read, Update and Delete Data
- Ways to Show a View
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using DevExpress.ExpressApp;
using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.BaseImpl;
using MySolution.Module.BusinessObjects;
namespace dxTestSolution.Module.BusinessObjects {
[DomainComponent]
public class MySearchClass : NonPersistentBaseObject {
[XafDisplayName("FirstName contains:")]
public string FirstName { get; set; }
[XafDisplayName("Age is equal to:")]
public int Age { get; set; }
public void SetContacts(IList<Contact> res) {
_contacts = res;
OnPropertyChanged(nameof(Contacts));
}
private IList<Contact> _contacts = new List<Contact>();
[XafDisplayName("Results:")]
public IList<Contact> Contacts {
get {
return _contacts;
}
}
}
}
C#using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using dxTestSolution.Module.BusinessObjects;
using MySolution.Module.BusinessObjects;
namespace dxTestSolution.Module.Controllers {
public class MySearchController : ObjectViewController<DetailView, MySearchClass> {
public MySearchController() {
var myAction1 = new SimpleAction(this, "MySearch", "MySearchCategory");
myAction1.Execute += MyAction1_Execute;
}
private void MyAction1_Execute(object sender, SimpleActionExecuteEventArgs e) {
var mySearchObject = (MySearchClass)View.CurrentObject;
var persistentOS = Application.CreateObjectSpace(typeof(Contact));
var criterion = CriteriaOperator.FromLambda<Contact>(x => x.FirstName.Contains(mySearchObject.FirstName) || x.Age == mySearchObject.Age);
var results = persistentOS.GetObjects<Contact>(criterion);
mySearchObject.SetContacts(results);
}
}
}
C#using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using dxTestSolution.Module.BusinessObjects;
using MySolution.Module.BusinessObjects;
namespace dxTestSolution.Module.Controllers {
public class MyShowSearchController : ObjectViewController<ListView, Contact> {
public MyShowSearchController() {
var mypopAction1 = new PopupWindowShowAction(this, "MyShowSearchAction", PredefinedCategory.Edit);
mypopAction1.TargetViewNesting = Nesting.Root;
mypopAction1.CustomizePopupWindowParams += MyAction1_CustomizePopupWindowParams;
}
private void MyAction1_CustomizePopupWindowParams(object sender, CustomizePopupWindowParamsEventArgs e) {
var nonPersistentOS = (NonPersistentObjectSpace)Application.CreateObjectSpace(typeof(MySearchClass));
var persistentOS = Application.CreateObjectSpace(typeof(Contact));
nonPersistentOS.AdditionalObjectSpaces.Add(persistentOS);
var obj = nonPersistentOS.CreateObject<MySearchClass>();
nonPersistentOS.CommitChanges();
var view = Application.CreateDetailView(nonPersistentOS, obj);
e.View = view;
}
}
}
Code<?xml version="1.0" encoding="utf-8"?>
<Application Title="ComplexSearchEF">
<SchemaModules>
<SchemaModule Name="SystemModule" Version="22.2.6.0" IsNewNode="True" />
</SchemaModules>
<Views>
<DetailView Id="MySearchClass_DetailView">
<Items>
<PropertyEditor Id="Age" Caption="Age is equal to" />
<PropertyEditor Id="Contacts" Caption="Results" />
<PropertyEditor Id="FirstName" Caption="FirstName contains" />
<ActionContainerViewItem Id="MySearchCategory" ActionContainer="MySearchCategory" Caption="MySearchCategory(8)" IsNewNode="True" />
<StaticText Id="ORItem" Text="OR" Caption="ORItem(10)" IsNewNode="True" />
</Items>
<Layout>
<LayoutGroup Id="Main" RelativeSize="100">
<LayoutGroup Id="SimpleEditors" RelativeSize="10.725806451612904">
<LayoutGroup Id="MySearchClass" RelativeSize="100">
<LayoutItem Id="FirstName" RelativeSize="28.571428571428573" />
<LayoutItem Id="ORItem" ViewItem="ORItem" Index="1" RelativeSize="17.293233082706767" IsNewNode="True" />
<LayoutItem Id="Age" Index="2" RelativeSize="22.55639097744361" />
<LayoutItem Id="MySearchCategory" ViewItem="MySearchCategory" Index="3" RelativeSize="31.57894736842105" IsNewNode="True" />
</LayoutGroup>
</LayoutGroup>
<LayoutGroup Id="Contacts_Group" Direction="Vertical" RelativeSize="89.2741935483871" Caption="Results:">
<LayoutItem Id="Contacts" RelativeSize="100" />
</LayoutGroup>
</LayoutGroup>
</Layout>
</DetailView>
</Views>
</Application>