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>
You could try changing the
StackPanelto aDockPanel(with appropriateDockPanel.Dockproperties 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).