1

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.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
BvdVen
  • 2,921
  • 23
  • 33
  • which element should be stretched depends on your algorithm, as your control acts as a layout container – thumbmunkeys Dec 07 '12 at 10:16
  • But I want the algorithm to "read" the xaml. So in this example TextA should be 10 pixels width and TextB should stretch... – BvdVen Dec 07 '12 at 10:19
  • as I said already, if a Width is specified the element does not stretch. so logically the other elements take up the remaining size. – thumbmunkeys Dec 07 '12 at 11:33
  • Yes that happens when you put that in a Grid or something... but now I have to define that width of both elements myself in the Measure/Arrange methods... – BvdVen Dec 07 '12 at 11:37

2 Answers2

1

Look at Horizontal and VerticalAlignment property of the control.

If it is not Stretch the control should only take the fixed size.

thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
  • I found that but on an UIElement there is no HorizontalAlignment or VerticalAlignment. When I cast the element to a TextBox is does, but the UIElement can be other elements to... – BvdVen Dec 07 '12 at 07:45
  • sorry... but I found out that the given XAML example both the textboxes are HorizontalAlignment stretch... – BvdVen Dec 07 '12 at 08:24
  • I think if you specify a width, then this overrides the Stretch setting Furthermore make sure to consider Min and MaxWidth when the setting is Stretch (and no explicit Width is set) – thumbmunkeys Dec 07 '12 at 09:30
  • But when I arrange the element myself I have to know this... I have to calculate the remaining width (in case of stretch element) or arrange width the fixed size – BvdVen Dec 07 '12 at 09:33
  • not sure what you mean... if you have a Width specified, then take this value. If a Min/Max is specified, then take what you need in between Min/Max. The alignment determines the positioning (top,left, bottom, etc) – thumbmunkeys Dec 07 '12 at 09:55
0

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

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
BvdVen
  • 2,921
  • 23
  • 33
  • But you had to cast it to `FrameworkElement` first, right? I am building a custom `Panel` implementation myself and have the same problem (and I would prefer _not_ to have to cast to FrameworkElement, since some of my child UIElements might be derived from UIElement directly). – d7samurai Dec 02 '13 at 17:37
  • Sorry I don't have the project anymore... so I can't check that for you. But I think thats right. – BvdVen Dec 04 '13 at 09:48