ArrangeOverride causes child items to be ordered before calling more code

Well, when I thought I had a layout system, I hit a brick wall ...

So, I have a WPF window with a custom ItemsControl element whose Items panel is a custom panel, and each ItemContainer is a custom element.

When a window calls InvalidateArrange (), the order of the calls to ArrangeOverride () seems fine, i.e.

Window -> List -> ItemsPanel -> ItemContainer -> ItemContainer -> etc... 

The problem is that I have code that I want to invoke at the Window level after ArrangeOverride, which relies on all ordered and dimensioned ItemContainers. The window level of ArrangeOverride () ends before the next element in the tree (list) has its ArrangeOverride.

Is there a way to measure all of the children of Windows and then return to Windows ArrangeOverride () to continue using some code, or is there some kind of connection event or something else?

Greetings

+6
c # layout wpf
source share
1 answer

You can call your custom code AFTER calling the .ArrangeOverride () database in your ArrangeOverride window:

  protected override Size ArrangeOverride(Size arrangeBounds) { Size temp = base.ArrangeOverride(arrangeBounds); // custom code goes here! return temp; } 

(I tested just now, and it works: custom code is executed after ArrangeOverride of any child control in the window)

EDIT: Running a proven and working example:

In the window 1.xaml.cs:

 [...] protected override Size ArrangeOverride(Size arrangeBounds) { Trace.TraceInformation("Window1.ArrangeOverride START"); Size temp = base.ArrangeOverride(arrangeBounds); Trace.TraceInformation("Window1.ArrangeOverride END"); return temp; } [...] 

In myUserControl.xaml.cs:

 [...] protected override Size ArrangeOverride(Size arrangeBounds) { Trace.TraceInformation("{0}.ArrangeOverride START", Tag); Size s = base.ArrangeOverride(arrangeBounds); Trace.TraceInformation("{0}.ArrangeOverride END", Tag); return s; } [...] 

In window 1.xaml:

 [...] <local:myUserControl Tag="FirstLevelControl"> <StackPanel> <local:myUserControl Tag="SecondLevelControl_1"> <TextBlock>First</TextBlock> </local:myUserControl> <local:myUserControl Tag="SecondLevelControl_2"> <TextBlock>Second</TextBlock> </local:myUserControl> </StackPanel> </local:myUserControl> [...] 

And finally, the result after execution:

 [...] Test.vshost.exe Information: 0 : Window1.ArrangeOverride START Test.vshost.exe Information: 0 : FirstLevelControl.ArrangeOverride START Test.vshost.exe Information: 0 : SecondLevelControl_1.ArrangeOverride START Test.vshost.exe Information: 0 : SecondLevelControl_1.ArrangeOverride END Test.vshost.exe Information: 0 : SecondLevelControl_2.ArrangeOverride START Test.vshost.exe Information: 0 : SecondLevelControl_2.ArrangeOverride END Test.vshost.exe Information: 0 : FirstLevelControl.ArrangeOverride END Test.vshost.exe Information: 0 : Window1.ArrangeOverride END [...] 

This demonstrates that a line of code between temp = base.ArrangeOverride and return temp always executed AFTER all the code in all ArrangeOverride methods of nested user controls. I did not try to track the execution of Arrange inline controls, but I believe that they have the same behavior.

+2
source share

All Articles