Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a child element limited in size by its parent in WPF?

I have a StackPanel which is larger than its parent Grid. I confirmed that using Snoop WPF Spy. The Widths are set to default. How to constrain it?

I don't like the ViewBox solution because it zooms out my text, and I want it to be wrapped/trimmed.

Edit the XAML as requested

<Grid>
    <StackPanel
        Orientation="Horizontal">
        <Border
            BorderBrush="Black"
            BorderThickness="1"
            Width="{ Binding BorderScreenShot }"
            Margin="0,-8,0,0">
            <Image
                ToolTip="Some Text"
                Cursor="Hand"
                Source="{ Binding Image }"
                Stretch="Fill"
                Visibility="{ Binding ImageVisibility }"
                MouseLeftButtonUp="Image_MouseLeftButtonUp" />
        </Border>
        <StackPanel
            Orientation="Vertical"
            Margin="10,0,0,0">
            <TextBlock
                Text="{ Binding Name }"
                TextWrapping="Wrap" />
            <TextBlock
                Text="{ Binding LongText }"
                TextWrapping="Wrap" />
            <StackPanel
                Orientation="Horizontal">
                <TextBlock
                    Text="{ Binding Category }" />
                <TextBlock
                    Text="{ Binding Version }" />
            </StackPanel>
        </StackPanel>
    </StackPanel>
</Grid>
like image 255
Jader Dias Avatar asked Jul 07 '11 13:07

Jader Dias


1 Answers

You could try changing the StackPanel to a DockPanel (with appropriate DockPanel.Dock properties on the children).

That will mimic the effect of the StackPanel but allow the children to be constrained.

Or, use a WrapPanel instead of a StackPanel.

StackPanels have infinite client size, so nothing inside a StackPanel will wrap or trim without explicit dimensions being set (instead, the StackPanel just grows to fit the contents).

like image 109
MarcE Avatar answered Sep 22 '22 10:09

MarcE