sorting elements in a stackpanel WPF

后端 未结 4 1013
春和景丽
春和景丽 2020-12-19 06:35

I have a stackpanel with some usercontrols that are added or removed during runtime. These elements have an index that i assign to them when i new them, I need to keep these

4条回答
  •  北海茫月
    2020-12-19 06:48

    The answer above is correct, but if you can not change your stackpanel (if you have not enough time, or have written many codes related to the stackpanel) try this:

    1. Store the controls in a List or Dictionary
    2. Sort the List or Dictionary
    3. Remove controls from stackpanel using : StackPanel.Children.Remove(child)
    4. Foreach member of List or Dictionary add controls to StackPanel using : StackPanel.Children.Insert(i, child);

    note: the code is working, Remove function removes the control from StackPanel item's (from the tree) but the control is already on the memory so that the control is able to inserting in any StackPanel or same of it.

提交回复
热议问题