wpf TextBlock TextWrapping未换行

monwx1rj  于 2023-03-24  发布在  其他
关注(0)|答案(6)|浏览(260)

当我把一个TextBlock放在一个水平对齐的StackPanel中时,它不会换行。我意识到这是因为StackPanel的可用宽度是PositiveInfinity,但是有什么解决方法吗?
我的布局比这个示例复杂得多,所以我不能删除StackPanel或Horizontal Orientation。

<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(解决方案):**将StackPanel替换为DockPanel。

<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>
s1ag04yj

s1ag04yj1#

这是因为您在StackPanel上使用了Horizontal方向。这意味着StackPanel为每个子控件提供了完整的 * 宽度 *,然后将它们水平放置-即使这意味着超过其有界/可见宽度。因为没有任何东西可以约束TextBlock的宽度,所以它不会换行。
如果你切换到垂直方向,那么 Package 工作,但我猜有一个原因,你指定否则。你能显示什么布局,你试图实现?

6tdlim6h

6tdlim6h2#

您可以使用网格代替StackPanel(如前所述,它不会限制其内容)。网格比StackPanel允许更多的项目布局控制,如果您的图像被折叠,则'auto'列将具有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>
7rfyedvj

7rfyedvj3#

我有几个解决这个问题的办法。
1)将父宽度绑定到文本块宽度。(在下面的情况下,我认为用户控件是父控件)。

<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>
nhn9ugyo

nhn9ugyo4#

如果你和我一样,想知道为什么在定义了宽度之后,文本块仍然没有换行。

尝试将MaxWidth添加到具有与Width相同值的Textblock,

这将在应用程序启动后停止控件的宽度增长。
希望这有帮助!

ercv8c1e

ercv8c1e5#

我遇到了同样的问题,但在我的情况下,我有一个带有ControlTemplate的文本框**样式。

TextWrapping="{TemplateBinding TextWrapping}"

为了控制模板的文本框采用我设置的值。

0yycz8jy

0yycz8jy6#

我在列表框中使用文本框
我必须将ScrollViewer.HorizontalScrollBarVisibility="Disabled"添加到ListBox,将TextWrapping="Wrap"添加到TextBox
代码示例:

<ListBox Name="ConsoleLogs" ScrollViewer.HorizontalScrollBarVisibility="Disabled" VerticalAlignment="Top" Width="Auto" Height="227" Grid.Column="1" ItemsSource="{Binding LogItems, UpdateSourceTrigger=PropertyChanged}">
   <ListBox.ItemTemplate >
      <DataTemplate>
         <DockPanel >
             <TextBlock Text="{Binding}" TextWrapping="Wrap"/>
         </DockPanel>
      </DataTemplate>
   </ListBox.ItemTemplate>

来源:DataTemplate in WPF: How to achive TextWrapping?

相关问题