views:

28

answers:

1

There is an attribute "AffectsParentArrange" that implicitly invalidates the parent's layout - but I want to attach an event handler to the "event" triggered by that attribute.

When a child property with the attribute changes, the parent's arrangement is invalidated.

I have a custom control (which is not the immediate parent) that needs to receive an event.

Does anyone know of an event that can be used here?

Do I need to provide a custom parent that overrides a method and fires an event?

A: 

It just calls InvalidateArrange() on the parent when the property change. This causes WPF to later call Arrange() on the parent. Neither of these calls has any way to intercept it (without using really sneaky techniques like patching IL code or attaching a debugger to the process).

This means that the only ways to write managed code that can detect the InvalidateArrange() is to create a custom parent which overrides the ArrangeOverride method, or tap into some behavior of the Arrange method for the particular control in question - for example if the parent is a Panel you could add an extra invisible child just to receive Arrange() calls from its parent.

Update When you get the Arrange() callback you cannot detect why InvalidateArrange() was called on the parent, but you can detect whether the property with the AffectsParentArrange flag has changed or not by saving the old value of the property in each Arrange call and comparing it to the previously saved value. This can be extended to multiple properties using a loop, a Dictionary, and either a list of properties or a call to GetLocalValueEnumerator().

Ray Burns
Problem is that ArrangeOverride can't differentiate between top-down and bottom-up. Sending an event from there would send every time the arrangement is invalidated from any source. Unfortunately, one of the side-effects of my event handler is to cause the control to be rearranged.
Jeff B
No problem. I've added the solution to this to my answer.
Ray Burns