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

I have a StackPanel that is larger than its parent Grid . I confirmed that using Snoop WPF Spy. Width set to the default value. How to limit it?

I do not like the ViewBox solution because it changes my text and I want it to be wrapped / cropped.

Change XAML on request

 <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> 
+6
source share
3 answers

You can try changing the StackPanel to a DockPanel (with the corresponding DockPanel.Dock properties for children).

This will mimic the effect of the StackPanel, but allow children to be limited.

Or use a WrapPanel instead of a StackPanel.

StackPanels have an infinite client size, so nothing inside the StackPanel will wrap or crop without setting explicit parameters (instead, the StackPanel just grows to fit the content).

+7
source

It is not possible for a child to be larger than its parent, unless its contents are larger. If you want to trim text, you can use TextBlock.TextTrimming = WordEllipsis/CharacterEllipsis . This will lead to the desired behavior if my interpretation of the question is correct.

However, this does not work with StackPanel . As the other answer suggests, you can use a DockPanel or Grid to host content.

Alternatively, you can wrap the StackPanel in a ScrollViewer and use the scroll bar to handle overflow.

Update This should fix it.

  <DockPanel> <Border DockPanel.Dock="Left" 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 DockPanel.Dock="Bottom" Orientation="Horizontal"> <TextBlock Text="{ Binding Category }" /> <TextBlock Text="{ Binding Version }" /> </StackPanel> <TextBlock DockPanel.Dock="Top" Text="{ Binding Name }" TextWrapping="Wrap" /> <TextBlock Text="{ Binding LongText }" TextTrimming="WordEllipsis" TextWrapping="Wrap" /> </DockPanel> 
0
source

If you do not want to compress the content, there is no other way to limit it - especially since the images are involved.

This pretty much leaves you with ScrollViewer , which was designed specifically for this scenario. Personally, I like to combine the ability to zoom in / out using the ScrollViewer function. This gives your users more options in how they view your content.

0
source

Source: https://habr.com/ru/post/892238/


All Articles