Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent parent from being resized by child

Tags:

layout

wpf

is there a possibility to prevent a textblock from resize it's parent? I have a textblock with a lot of text and I want it to wrap, but not to enlarge the parents size. The size of the parent may be variable. Greetings Thomas

like image 414
freakinpenguin Avatar asked Sep 04 '11 17:09

freakinpenguin


People also ask

How do I stop DIV from resizing?

It is mostly used with textarea and div elements. To disable resizable property of an element use resize to none. It is the default value.

Which of the following is a common Method for keeping a parent element from collapsing when it contains only floated children?

Method 1 (Using Overflow Property): We can use the overflow property of CSS to prevent the parents from collapsing. Set the value of the overflow property as “auto” for the parent and it will not collapse any more.

What is the function of the resize attribute?

The resize property defines if (and how) an element is resizable by the user. Note: The resize property does not apply to inline elements or to block elements where overflow="visible". So, make sure that overflow is set to "scroll", "auto", or "hidden".


2 Answers

Unfortunately there is no Property for this feature.

The only workaround that I'm aware of is to either use an existing control or place another hidden control in the same space and then bind Width of your TextBlock/TextBox to the ActualWidth of that control.

Here is an example when the TextBlock doesn't effect the Width of the ColumnDefinition but it will get wider if the ColumnDefinition is resized for another reason

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Column="1"
               TextWrapping="Wrap"
               Text="{Binding ...}"
               Width="{Binding ElementName=sizingControl, Path=ActualWidth}"/>
    <Rectangle Name="sizingControl" Grid.Column="1" Visibility="Hidden" />
</Grid>
like image 150
Fredrik Hedblad Avatar answered Oct 14 '22 10:10

Fredrik Hedblad


For this to work, you need to set a width on the parent or place another grid or some container inside the parent which holds the textblock. Then set a width on that. The textblock will not wrap on a flexible parent.

Or better yet, just set a width on the textblock.

like image 30
Shawn Mclean Avatar answered Oct 14 '22 10:10

Shawn Mclean