防止父母为孩子调整大小 [英] Prevent parent from being resized by child
问题描述
是否有可能阻止文本块调整其父级的大小? 我有一个包含大量文本的文本块,并且希望将其包装,但不希望扩大父母的大小.父母的大小可以是可变的. 托马斯的问候
不幸的是,此功能没有属性.
我知道的唯一解决方法是使用现有控件或将另一个隐藏控件放在同一空间中,然后将TextBlock/TextBox
的Width
绑定到该控件的ActualWidth
.>
以下是TextBlock
不影响ColumnDefinition
的Width
的示例,但是如果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屋!