When working with XAF (eXpressApp Framework) and using DxTreeListEditor (from the DevExpress Blazor components), it's a common requirement to automatically expand all tree nodes when new items are created or added.
Here is a general approach to make sure all items in the DxTreeList (used by DxTreeListEditor) are expanded when new items are created:
✅ Steps to Expand All Nodes Upon Creating New Items
- Override the ViewController for your
DxTreeListEditor view.
- Handle the Object Space's Created event or the collection's
ListChanged/ObjectChanged event.
- Use the
ExpandAll() method in the DxTreeListEditor.TreeList native control to programmatically expand.
🔧 Sample Implementation
Here’s how you could implement this:
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Blazor.Editors;
using DevExpress.ExpressApp.Blazor.SystemModule;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp.Trees.Blazor.Editors;
using DevExpress.Blazor;
using System;
public class ExpandAllNodesController : ViewController<ListView> {
DxTreeListEditor treeListEditor;
protected override void OnActivated() {
base.OnActivated();
// Only proceed if the editor is DxTreeListEditor
treeListEditor = View.Editor as DxTreeListEditor;
if (treeListEditor != null) {
// Attach to ObjectSpace.ObjectChanged event
ObjectSpace.ObjectChanged += ObjectSpace_ObjectChanged;
// Optional: Expand all right after list loaded
View.ObjectSpace.Committed += ObjectSpace_Committed;
}
}
private void ObjectSpace_ObjectChanged(object sender, ObjectChangedEventArgs e) {
if (e.Reason == ObjectChangeReason.NewObject) {
ExpandAllNodes();
}
}
private void ObjectSpace_Committed(object sender, EventArgs e) {
ExpandAllNodes();
}
private void ExpandAllNodes() {
if (treeListEditor != null && treeListEditor.TreeList != null) {
treeListEditor.TreeList.ExpandAll();
}
}
protected override void OnDeactivated() {
base.OnDeactivated();
if (treeListEditor != null) {
ObjectSpace.ObjectChanged -= ObjectSpace_ObjectChanged;
ObjectSpace.Committed -= ObjectSpace_Committed;
}
}
}
💡 Notes
treeListEditor.TreeList.ExpandAll() is the key method that expands all nodes.
- You could also use the
treeListEditor.TreeList.CollapseAll() to collapse if needed.
- Make sure that
treeListEditor.TreeList is not null before calling ExpandAll().
- You may need to adjust the trigger depending on whether the item is being added directly via UI or programmatically.
🔄 Alternative Trigger Points
If you want to trigger expansion after saving a new record or every time the list is refreshed, you can also handle:
ListView.CollectionSource.CollectionChanged
View.CurrentObjectChanged
ListView.Editor.ControlsCreated
Let me know your specific use-case (e.g., inline creation, detail view save-and-close, etc.), and I can tailor the approach even more precisely.