防止父母为孩子调整大小 [英] Prevent parent from being resized by child

查看:55
本文介绍了防止父母为孩子调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能阻止文本块调整其父级的大小? 我有一个包含大量文本的文本块,并且希望将其包装,但不希望扩大父母的大小.父母的大小可以是可变的. 托马斯的问候

解决方案

不幸的是,此功能没有属性.

我知道的唯一解决方法是使用现有控件或将另一个隐藏控件放在同一空间中,然后将TextBlock/TextBoxWidth绑定到该控件的ActualWidth.

以下是TextBlock不影响ColumnDefinitionWidth的示例,但是如果ColumnDefinition由于其他原因调整大小,它将变得更宽

<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>

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

解决方案

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>

这篇关于防止父母为孩子调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆