Reputation: 15282
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.
<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>
Upvotes: 0
Views: 1022
Reputation: 20746
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.
Upvotes: 1
Reputation: 1461
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
Upvotes: 0
Reputation: 3461
You could stuff all of your children into DockPanel
controls, make sure they are the only child and then set the LastChildFill
property 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.
Upvotes: 0
Reputation: 9565
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. :)
Upvotes: 0