在运行时在另一个 LayoutControlGroup 中重新排序 LayoutControlGroups

Reorder LayoutControlGroups inside another LayoutControlGroup on runtime

我有 LayoutControlGroup 和其他几个 LayoutControlGroup

我需要做的是在运行时将它们重新排序为特定顺序。

例如,当表格出现时,我按以下顺序得到这些组:

[Group0]
[Group1]
[Group2]
[Group3]
[Group4]

然后我从数据库中获取了一些数据,需要根据这些数据对组进行重新排序,例如只是颠倒顺序:

[Group4]
[Group3]
[Group2]
[Group1]
[Group0]

我已经尝试过的:

List<BaseLayoutItem> currentOrder = lcgParentGroup.Items.Cast<object>().Where(c => c is BaseLayoutItem).Cast<BaseLayoutItem>().Where(c => c.Name.StartsWith("lcgInnerGroup") && char.IsDigit(c.Name.Last())).ToList();
currentOrder.Sort((x, y) => System.Collections.Comparer.Default.Compare(x.Location.Y, y.Location.Y));

// Example order
List<BaseLayoutItem> requiredOrder = new List<BaseLayoutItem>(currentOrder);
requiredOrder.Reverse();

for (int i = 1; i < requiredOrder.Count; ++i)
{
    // I just move every group after every relative group.
    BaseLayoutItem relative = lcgParentGroup.Items.FindByName(requiredOrder[i - 1].Name);

    DevExpress.XtraLayout.Customization.LayoutItemDragController dragController = new DevExpress.XtraLayout.Customization.LayoutItemDragController(requiredOrder[i], relative, MoveType.Outside, InsertLocation.After, LayoutType.Vertical);
    bool result = requiredOrder[i].Move(dragController);    
}

但是每次 result 都是 false

还有其他方法吗?

我已经弄明白了。这些组必须是可见的 (Visibility.Always) 才能移动它们。不知道为什么。

解决伪代码:

try
{
    groups.BeginUpdate();

    List<object> dataFromDB = ...;

    // Znamy aktualną kolejność grup
    List<BaseLayoutItem> currentOrder = groups.Items.Cast<object>().Where(c => c is BaseLayoutItem).Cast<BaseLayoutItem>().ToList();

    // Sort as visibile in form
    currentOrder.Sort((x, y) => System.Collections.Comparer.Default.Compare(x.Location.Y, y.Location.Y));

    // Save old visibilities
    LayoutVisibility[] allVisibilities = currentOrder.Select(x => x.Visibility).ToArray();
    currentOrder.ForEach(x => x.Visibility = LayoutVisibility.Always);

    List<BaseLayoutItem> requiredOrder = ...; // prepare required order

    // Make requiredOrder[0] topmost group

    // Move
    for (int i = 1; i < requiredOrder.Count; ++i)
    {
        BaseLayoutItem relative = groups.Items.FindByName(requiredOrder[i - 1].Name);
        BaseLayoutItem toMove = groups.Items.FindByName(requiredOrder[i].Name);

        toMove.Move(relative, InsertType.Bottom);
    }

    // Restore visibilities
    for (int i = 0; i < currentOrder.Count; ++i)
    {
        currentOrder[i].Visibility = allVisibilities[i];
    }

}
finally
{
    groups.EndUpdate();
}