In WPF how can I get the rendered size of a control before it actually renders? In WPF how can I get the rendered size of a control before it actually renders? wpf wpf

In WPF how can I get the rendered size of a control before it actually renders?


In general, you should be able to get the correct size from ArrangeOverride. This doesn't include things like Margin, but that probably shouldn't be taken into account. You could either use the size passed as a parameter as your "render" size or use the return value of the base.ArrangeOverride call.

EDIT:

The OnRender method is called from the Arrange method, after OnArrangeOverride is ultimately called. The OnRenderSizeChanged on the other hand is called from UpdateLayout, which is effectively dispatched to be executed all at once for a given section of the visual tree. This is why the OnRenderSizeChanged is called after the OnRender.

The documentation may refer to the "rendering" as in actually rendered to the screen, not when OnRender is called. WPF can cache the rendering instructions for a given element and execute them when needed. So the fact that OnRender is called before OnRenderSizeChanged, doesn't mean it's actual rendering instructions are committed to the screen at that time.

You can modify your OnRenderSizeChanged to force OnRender to be called again using:

protected override void OnRenderSizeChanged(System.Windows.SizeChangedInfo sizeInfo){    Console.WriteLine("OnRenderSizeChanged Entered");    base.OnRenderSizeChanged(sizeInfo);    this.InvalidateVisual();    Console.WriteLine("OnRenderSizeChanged Exited");}

You may also want to skip your OnRender code if RenderSize is "0,0".