开发者

Prevent parent from being resized by child

is there a possi开发者_开发问答bility 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


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜