Search code examples
wpfxamluielementmeasure

How to know if UIElement has fixed width or should stretch in measure/arrange


I have made a control (looks like a grid) that can contain multiple children (UIElements). I measure and arrange the whole layout in code. Now some of these elements have defined a width in xaml and some doesn't have that.

How do I know in the measureOverride/arrangeoverride if a element has an fixed width or should use the whole available width?

Example XAML:

<MyControl>
   <TextBox x:Name="TextA" Text="TextFixed" Width="10"/>
   <TextBox x:Name="TextB" Text="TextStretch"/>
</MyControl>

Inside the MeasureOverride method after the uielement.Measure() the size is the minimal needed size for that element. I don't know how to see when I should calculate the available width for "TextB"...

Update:

In the MeasureOverride() both the elements have HorizontalAlignment 'Stretch' and after uiElement.Measure(availableSize) both have a DesiredSize.Width. That width is the minimal width it needs. With this information I am not able to know which of the two elements should be stretched or not.


Solution

  • The solution for my problem was to do a check in the ArrangeOverride() on the width when an element is supposed to stretch the width is double.NaN