I have a Window with a Border control inside.
The Border is a placeholder for different views/user controls.
First view is a user control with a data grid. The data grid works fine. I can sort columns by clicking column header.
Than I exchange the borders child with another user control for different use case. This works also fine.
When I now exchange the border child back to the first data grid user control, my sorting got lost.
Lets say the second column was sorted descending before switching, than the first column is sorted ascending when I switch back to it.
I am caching the data grid user control, which means, I do not destroy it and create a new one.
So how can I keep my sort order when switching back to it?
I found a solution:
Instead of unsing a Border control and exchanging the Borders Child, I will use a StackPanel. When the view has to be "switched" I "collapse" (a term of WPF visiblity enums) the old view and make the new view visible.
When toggling back I do it vice versa.
Thus the parent/child relationship is kept and all sort orders and selection states are kept.