当我将TextBlock放置在水平对齐的StackPanel内部时,它不会换行。我意识到这是因为StackPanel的可用宽度是PositiveInfinity但是有没有解决办法?
我的布局比这个示例复杂得多,所以我无法删除StackPanel或 水平方向。我只是试图重现展示行为的最简单的例子。
<StackPanel Orientation="Horizontal">
<Rectangle Width="50" Height="50" Fill="Blue" VerticalAlignment="Top" />
<Rectangle Width="50" Height="50" Fill="Red" VerticalAlignment="Top" />
<TextBlock TextWrapping="Wrap"
Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus leo lectus, viverra ut lobortis vel, mollis eget lectus. Suspendisse laoreet consequat ultrices. Curabitur ultricies, tortor feugiat porttitor faucibus, lorem eros pretium nisl, eu ullamcorper mauris tortor sit amet augue." />
</StackPanel>
更新:TextBlock的宽度必须是动态的。我需要它随着窗口大小调整而流动。
更新2:向StackPanel添加了另一个元素,因为我需要水平布置子项。
更新3(解决方案):使用DockPanel替换StackPanel。
<DockPanel>
<DockPanel DockPanel.Dock="Top">
<Rectangle Width="50" Height="50" Fill="Blue" VerticalAlignment="Top" DockPanel.Dock="Left" />
<Rectangle Width="50" Height="50" Fill="Red" VerticalAlignment="Top" DockPanel.Dock="Left" />
<TextBlock TextWrapping="Wrap"
Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus leo lectus, viverra ut lobortis vel, mollis eget lectus. Suspendisse laoreet consequat ultrices. Curabitur ultricies, tortor feugiat porttitor faucibus, lorem eros pretium nisl, eu ullamcorper mauris tortor sit amet augue." />
</DockPanel>
</DockPanel>
答案 0 :(得分:44)
这是因为您在StackPanel上使用水平方向。这意味着StackPanel为每个子控件提供完整的 width ,然后将它们水平放置 - 即使这意味着超出其有界/可见宽度。因为没有什么可以限制TextBlock的宽度,所以它不会换行。
如果你切换到垂直方向然后包装工作,但我猜你有一个原因,你否则指定。你能展示一下你想要实现的布局吗?
答案 1 :(得分:25)
您可以使用网格而不是StackPanel(正如所解释的那样,不限制其内容)。网格允许对项目布局的控制比StackPanel更多,如果图像折叠,则“自动”列的宽度为0。
<DockPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Rectangle Width="50" Height="50" Fill="Blue" VerticalAlignment="Top" />
<TextBlock TextWrapping="Wrap" Grid.Column="1"
Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus leo lectus, viverra ut lobortis vel, mollis eget lectus. Suspendisse laoreet consequat ultrices. Curabitur ultricies, tortor feugiat porttitor faucibus, lorem eros pretium nisl, eu ullamcorper mauris tortor sit amet augue." />
</Grid>
</DockPanel>
答案 2 :(得分:8)
我为这个问题找到了几个解决方案。
1)将父宽度绑定到文本块宽度。 (以下情况我将usercontrol视为父级)。
<UserControl x:Class="WpfApplication1.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" Name="userctrl">
<DockPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top" HorizontalAlignment="Left" >
<TextBlock TextWrapping="Wrap" MaxWidth="{Binding ElementName=userctrl,Path=ActualWidth}"
Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus leo lectus, viverra ut lobortis vel, mollis eget lectus. Suspendisse laoreet consequat ultrices. Curabitur ultricies, tortor feugiat porttitor faucibus, lorem eros pretium nisl, eu ullamcorper mauris tortor sit amet augue." />
</StackPanel>
</DockPanel>
</UserControl>
2)其他解决方案是使用Grid而不是stackpanel
<UserControl x:Class="WpfApplication1.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" Name="userctrl">
<DockPanel>
<Grid DockPanel.Dock="Top" HorizontalAlignment="Left" >
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock TextWrapping="Wrap"
Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus leo lectus, viverra ut lobortis vel, mollis eget lectus. Suspendisse laoreet consequat ultrices. Curabitur ultricies, tortor feugiat porttitor faucibus, lorem eros pretium nisl, eu ullamcorper mauris tortor sit amet augue." />
<TextBlock TextWrapping="Wrap" Grid.Column="1" Margin="20 0 0 0"
Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus leo lectus, viverra ut lobortis vel, mollis eget lectus. Suspendisse laoreet consequat ultrices. Curabitur ultricies, tortor feugiat porttitor faucibus, lorem eros pretium nisl, eu ullamcorper mauris tortor sit amet augue." />
</Grid>
</DockPanel>
</UserControl>
答案 3 :(得分:2)
如果你像我一样,并想知道为什么Textblock在定义宽度后仍未包装。
尝试向Textblock添加MaxWidth,其值与Width相同,
一旦应用程序启动,这将阻止控件在Width中增长。
希望这有帮助!