WPF 布局:自动换行不起作用

发布于 2024-07-12 21:40:03 字数 2078 浏览 7 评论 0原文

即使我指定了自动换行,为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

于我来说 2024-07-19 21:40:04

它位于画布内部,因此没有设置任何宽度。 如果您不需要画布,请将其更改为网格(它会自动调整自身大小),并且 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文