Example E1807
Visible to All Users

XAF - How to display a collection property as a checked list box

This example illustrates how to display a collection property as a checked list box in XAF applications.

image

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 the Master object using checkboxes.

Implementation Details

  1. Implement Master and Detail business object classes, where Master contains a collection of Detail objects.
  2. Copy the implementation of one of the following custom property editors:
  3. Run the application.
  4. Create several Detail objects, then create a new Master object. You can link/unlink existing Detail objects using editor checkboxes.

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

EFCore/CheckedListEF/CheckedListEF.Module/BusinessObjects/Master.cs
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>(); } }
EFCore/CheckedListEF/CheckedListEF.Module/BusinessObjects/Detail.cs
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); } } }
EFCore/CheckedListEF/CheckedListEF.Win/Editors/WinCheckedListBoxPropertyEditor.cs
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 } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.