防止父母被孩子调整大小

时间:2021-02-11 22:21:23

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

是否有可能阻止文本块调整其父级的大小?我有一个包含大量文本的文本块,我想要它包装,但不要扩大父母的大小。父级的大小可以是变量。问候托马斯

2 个解决方案

#1


5  

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.

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

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

下面是一个示例,当TextBlock不影响ColumnDefinition的宽度时,如果由于其他原因调整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>

#2


-1  

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.

或者更好的是,只需在文本块上设置宽度即可。

#1


5  

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.

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

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

下面是一个示例,当TextBlock不影响ColumnDefinition的宽度时,如果由于其他原因调整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>

#2


-1  

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.

或者更好的是,只需在文本块上设置宽度即可。