Basically I want to lay out visual elements that can change their content according to location where they would be laid. I want to do it in WPF but you might be able to suggest general algorithm without knowing much about WPF.
Example: Consider WrapPanel
that contains only Label
s that hold some text. When a Label
is laid by WrapPanel
at the beginning of new row then all the leading white spaces will be removed from its text. That will indeed make the Label
shorter.
The only think that knows about where exactly will be such Label
laid is WrappPanel
that does layouting. So one approach to do this is to code custom Panel
that overrides MeasureOverride
and ArrangeOverride
and change Label
s inside MeasureOverride
before calling Measure
on the Label
. I don't like this because:
- It is ugly hack (
Panel
and especiallyMeasureOverride
andArrangeOveride
should only position children not change them). - I'm not sure but I think that if I changed a
Label
insideMeasureOverride
theLabel
would cause layout to be updated in response which would lead to another (recursive) call toMeasureOverride
(=> infinite loop).
So please suggest the right design for this scenario. How should it do this? One that comes to my mind is to:
- Have regular
WrapPanel
. - Every times its
Width
orHeight
is changed or some children is changed (its children might be repositioned) update children beforeMeasureOverride
andArrangeOverride
is called. - During the process of updating children (2.) no call to
Measure
orArrange
of theWrapPanel
can be made. Question: How do I temporally inhibit all such calls ?
Before you suggest that I use RichTextBox
or TextBlock
deal with white space at the beginning of the lines note that Label
s and white spaces are just an example - it is little more complicated. But If I know how to solve 'Label
s and white spaces' correctly I'll know how to solve real problem too.
If you want to know why do I need do this have a look on my question Create guitar chords editor in WPF and H.B.'s answer.