This example illustrates how to display a collection property as a checked list box in XAF applications.
The example application:
- Shows the collection of all existing
Detail
objects as a checked list box. - Allows you to link/unlink child
Detail
records to/from theMaster
object using checkboxes.
Implementation Details
- Implement Master and Detail business object classes, where
Master
contains a collection ofDetail
objects. - Copy the implementation of one of the following custom property editors:
- For Blazor applications, use CheckedListBoxEditor.
- For Windows Forms applications, use WinCheckedListBoxPropertyEditor.cs.
- Run the application.
- Create several
Detail
objects, then create a newMaster
object. You can link/unlink existingDetail
objects using editor checkboxes.
Files to Review
- Blazor - CheckedListBoxEditor
- Windows Forms - WinCheckedListBoxPropertyEditor.cs
Documentation
More Examples
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
using System.Collections.ObjectModel;
namespace CheckedListEF.Module {
[DefaultClassOptions]
public class Master : BaseObject {
public virtual string MasterName { get; set; }
public virtual IList<Detail> Details { get; set; } = new ObservableCollection<Detail>();
}
}
C#using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
using DevExpress.Xpo;
namespace CheckedListEF.Module {
[DefaultClassOptions]
public class Detail : BaseObject , IComparable {
public virtual string DetailName { get; set; }
public int CompareTo(object obj) {
return this.ID.CompareTo(((Detail)obj).ID);
}
}
}
C#using System;
using DevExpress.ExpressApp.Win.Editors;
using DevExpress.ExpressApp;
using DevExpress.XtraEditors;
using System.Windows.Forms;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Model;
using CheckedListEF.Module;
using DevExpress.Persistent.BaseImpl.EF;
using System.Collections.ObjectModel;
using System.Collections;
namespace CheckedList.Module.Win {
[PropertyEditor(typeof(IList<Object>), false)]
public class WinCheckedListBoxPropertyEditor : WinPropertyEditor, IComplexViewItem {
public WinCheckedListBoxPropertyEditor(Type objectType, IModelMemberViewItem model) : base(objectType, model) { }
protected override object CreateControlCore() {
return new CheckedListBoxControl();
}
IList checkedItems;
XafApplication application;
IObjectSpace objectSpace;
protected override void ReadValueCore() {
base.ReadValueCore();
Control.ItemCheck -= new DevExpress.XtraEditors.Controls.ItemCheckEventHandler(control_ItemCheck);
checkedItems = (IList)PropertyValue;
List<Object> dataSource = objectSpace.GetObjects(MemberInfo.ListElementTypeInfo.Type).Cast<Object>().ToList();
Control.DataSource = dataSource;
IModelClass classInfo = application.Model.BOModel.GetClass(MemberInfo.ListElementTypeInfo.Type);
Control.DisplayMember = classInfo.DefaultProperty;
foreach (object obj in checkedItems) {
Control.SetItemChecked(dataSource.IndexOf(obj), true);
}
Control.ItemCheck += new DevExpress.XtraEditors.Controls.ItemCheckEventHandler(control_ItemCheck);
// }
}
void control_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e) {
object obj = Control.GetItemValue(e.Index);
switch (e.State) {
case CheckState.Checked:
checkedItems.Add(obj);
break;
case CheckState.Unchecked:
checkedItems.Remove(obj);
break;
}
OnControlValueChanged();
objectSpace.SetModified(CurrentObject);
}
public new CheckedListBoxControl Control {
get {
return (CheckedListBoxControl)base.Control;
}
}
#region IComplexPropertyEditor Members
public void Setup(IObjectSpace objectSpace, XafApplication application) {
this.application = application;
this.objectSpace = objectSpace;
}
#endregion
}
}