Search code examples
wpfwpf-controlsmeasureoverride

Customized Measure / Arrange algorithms on controls that use Data Binding in WPF


I'm writing an arrange / measure algorithm for a custom editor. The editor uses custom UserControl-derived classes as controls on the canvas. These controls are data bound to various properties / lists on my data model objects. The measuring and arranging of the custom controls is highly dependent upon the data being bound (because the data affects the size of the controls) and also dependent on the order in which the controls are measured and arranged. From what I've been able to determine from the WPF documentation on MSDN, Data binding isn't actually performed until after controls are measured and arranged, and immediately before the control is marked as loaded. As far as I can tell, this means that I have to wait for all of my controls to be Loaded before I can actually measure and arrange my custom canvas. As a result, I find myself having to set flags in my canvas to determine when I can and can't measure / arrange it, and using this flags to force invalidation and redraw. Is there a better way for customized measurement / arrangement on a canvas that I'm just not aware of ? Is there anyway to force early data binding in WPF ?


Solution

  • I've had to workaround the data binding limitation by doing a check in my [Measure|Arrange]Override methods to ensure that all of the items have been Loaded and have their ItemSources and DataContexts populated prior to actually measuring and arranging the controls. If somebody ever finds a better method of doing this, can they please post it here ? It'd be much appreciated.