0

I'm not sure how to go about this, but I created a custom Panel FooPanel. I want my FooPanel to force all the children to fit within a size that is predetermined by the panel (that way they are uniform). I know about UniformGrid but I also have some other stuff going into it.

I'm not sure how to force the children to fit within the box I tell them to.


Child.xaml:
<Style TargetType="{x:Type l:FooChild}">
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="Padding" Value="12,2,12,2" />
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="Background" Value="White" />
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="VerticalContentAlignment" Value="Stretch" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type l:FooChild}">
                <Border x:Name="ChromeBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                    <ContentPresenter ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                      HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
                                      VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
                                      />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
michael
  • 14,844
  • 28
  • 89
  • 177

4 Answers4

1

In your custom panel you can only give the children some space. After that, it is up to child elements how to align themselves in that space. This behavior is controlled by the HorizontalAlignment/VerticalAlignment properties of a FrameworkElement. If both of those properties are set to Stretch then the element will stretch taking all the space you gave to them inside your panel.

Update:

In addition to HorizontalContentAllignment/VerticalContentAlignment set the HorizontalAlignment/VerticalAlignment properties to Stretch in the style of FooChild element.

Pavlo Glazkov
  • 20,498
  • 3
  • 58
  • 71
0

Did you try setting HorizontalAlignment="Stretch" or HorizontalContentAlignment="Stretch" (and there's a VertialAlignment)

Could you provide some xaml? I may be able to help you better that way. :)

Ashley Grenon
  • 9,305
  • 4
  • 41
  • 54
0

You could stuff all of your children into DockPanel controls, make sure they are the only child and then set the LastChildFillproperty to True.

Alternatively, you could override the MeasureOverride property on all of your children and force them all to expand to the maximum amount of space available to them.

Dave White
  • 3,451
  • 1
  • 20
  • 25
  • I can't use a DockPanel because I am creating a custom panel. I realize there are other panels that are close to what I want, but they aren't exactly what I want, which is the reason why I chose to go with a custom panel. It just happens to be that one part of my custom panel requires that the children all be the same size. If I could, I'd use a `UniformGrid` instead, but it doesn't provide exactly what I need. It is close though... – michael Apr 06 '11 at 14:15
0

I had a problem like this only a couple of weeks ago. I fixed it like this, but I noticed it's not the preferred method.

    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListBox.ItemContainerStyle>

This is for a ListBox but I imagine it works for any ItemsControl.

Source: another SO question

Community
  • 1
  • 1
Jonas Van der Aa
  • 1,441
  • 12
  • 27