WPF 布局:自动换行不起作用
即使我指定了自动换行,为什么 TextBlock 中的文本会向右延伸到画布之外?
<Window x:Class="WpfApplication6.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" SizeToContent="WidthAndHeight">
<DockPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<Button Content="111"/>
<Button Content="222"/>
<Button Content="333"/>
<Button Content="444"/>
<Button Content="555"/>
</StackPanel>
<StackPanel DockPanel.Dock="Left">
<Button Content="One"/>
<Button Content="Two"/>
<Button Content="Three"/>
<Button Content="Four"/>
<Button Content="Five"/>
</StackPanel>
<Canvas Background="tan">
<TextBlock TextWrapping="Wrap">This is the content in this area here</TextBlock>
</Canvas>
</DockPanel>
</Window>
解决方案:谢谢,史蒂夫,做到了,我还添加了一个 ScrollViewer,很好:
<DockPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<Button Content="111"/>
<Button Content="222"/>
<Button Content="333"/>
<Button Content="444"/>
<Button Content="555"/>
</StackPanel>
<StackPanel DockPanel.Dock="Left">
<Button Content="One" Click="Button_Click" />
<Button Content="Two"/>
<Button Content="Three"/>
<Button Content="Four"/>
<Button Content="Five"/>
</StackPanel>
<Grid Background="tan">
<ScrollViewer>
<TextBlock Name="mainArea" Padding="10" TextWrapping="Wrap">This is the content in this area here</TextBlock>
</ScrollViewer>
</Grid>
</DockPanel>
Why does the text in my TextBlock extend out to the right beyond my canvas even though I specified word wrap?
<Window x:Class="WpfApplication6.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" SizeToContent="WidthAndHeight">
<DockPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<Button Content="111"/>
<Button Content="222"/>
<Button Content="333"/>
<Button Content="444"/>
<Button Content="555"/>
</StackPanel>
<StackPanel DockPanel.Dock="Left">
<Button Content="One"/>
<Button Content="Two"/>
<Button Content="Three"/>
<Button Content="Four"/>
<Button Content="Five"/>
</StackPanel>
<Canvas Background="tan">
<TextBlock TextWrapping="Wrap">This is the content in this area here</TextBlock>
</Canvas>
</DockPanel>
</Window>
SOLUTION: Thanks, Steve, that did it, I added a ScrollViewer as well, nice:
<DockPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<Button Content="111"/>
<Button Content="222"/>
<Button Content="333"/>
<Button Content="444"/>
<Button Content="555"/>
</StackPanel>
<StackPanel DockPanel.Dock="Left">
<Button Content="One" Click="Button_Click" />
<Button Content="Two"/>
<Button Content="Three"/>
<Button Content="Four"/>
<Button Content="Five"/>
</StackPanel>
<Grid Background="tan">
<ScrollViewer>
<TextBlock Name="mainArea" Padding="10" TextWrapping="Wrap">This is the content in this area here</TextBlock>
</ScrollViewer>
</Grid>
</DockPanel>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它位于画布内部,因此没有设置任何宽度。 如果您不需要画布,请将其更改为网格(它会自动调整自身大小),并且 TextBlock 将正确换行。
It's inside a Canvas, so it's not getting any width set. If you don't need the canvas, change it to a Grid (which automatically sizes itself) and the TextBlock will wrap properly.