tags:

views:

1113

answers:

1

I have a custom Silverlight control that extends Panel. I require the data template (root node is stack panel) to take 100% of the available width - at present, however, the stack panel only fills to the width of the text.

The arrange override in my control:

protected override Size MeasureOverride(Size availableSize)
{
    foreach (FrameworkElement element in Children)
    {
        element.Measure(new Size(availableSize.Width, availableSize.Height));
    }
    return base.MeasureOverride(availableSize);
}

And my template:

<DataTemplate x:Name="itemTemplate">
    <StackPanel Background="Aqua" HorizontalAlignment="Stretch">
        <TextBlock TextWrapping="Wrap" Text="{Binding Name}" HorizontalAlignment="Stretch"/>
    </StackPanel>
 </DataTemplate>

Any help would be greatly appreciated.

A: 

Add Width="*" to the StackPanel

Oops that works in Grid Row, column definitions.

Why are you overriding the Measure code anyway? The auto behaviour of Width is to use the available space. Similarly Width on panel is the standard FrameworkElement Width property.

AnthonyWJones
I get the following error when I do that."Invalid attribute value * for property Width."This occurs in my create children routine at: FrameworkElement element = ItemTemplate.LoadContent() as FrameworkElement;
fturtle
"*" is not valid for a Width or Height property.
tucod
The children aren't rendered unless I override MeasureOverride. I'm following this resource to some extent:http://msdn.microsoft.com/en-us/library/cc903936(VS.95).aspx
fturtle