tags:

views:

22

answers:

1

Imagine I have two WPF FrameworkElements that need to be laid out. One element contains text, and the other shows line numbers for the text.

The widths of these two elements are not independent. For example, if the text element gets narrower, then more lines may wrap, which increases the number of lines, which may cause the line number element to grow wider (e.g. if the number of lines goes from 99 to 100). But if the line number element grows wider, then there's less space for the text element, etc.

How do I implement layout in WPF if the sizes of my child elements need to be calculated in concert with one another? I am new to WPF layout, but it looks like a fundamental assumption of MeasureOverride is that an element's size can be calculated without regard to the sizes of peers.

Thanks for any help.

+1  A: 

Sounds to me you need to implement the layout in the container. See http://www.wpftutorial.net/CustomLayoutPanel.html on how to implement your own layout panel. I think a Grid layout can do what you want. See http://www.wpftutorial.net/GridLayout.html

Lars Truijens
I agree, trying to fiddle with independent elements is going to cause lots of headaches, whereas a simple container can deal with most of these issues automatically.
IanGilham