TextBlock TextWrapping 不包含在 StackPanel 中 [英] TextBlock TextWrapping not wrapping inside StackPanel

查看:27
本文介绍了TextBlock TextWrapping 不包含在 StackPanel 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 StackPanel,但是下面这行:

I have a StackPanel, but the following line :

<TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Notes}" TextWrapping="Wrap"  />

不包装文本.

<StackPanel Orientation="Vertical">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="15" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <DockPanel Grid.Row="0" Grid.Column="0">
            <TextBlock FontWeight="Bold" Padding="0,0,5,0" Text="{Binding Path=Id, StringFormat='#\{0\}'}" />
            <TextBlock FontWeight="Bold" Padding="0,0,5,0" Text="{Binding Path=Name}" />
        </DockPanel>
        <TextBlock Grid.Row="0" Grid.Column="4" FontWeight="Bold" Text="{Binding Path=Time, StringFormat={}{0:HH:mm}}" />
        <Image
            Grid.Row="0"
            Grid.Column="6"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Source="{Binding Path=Image, Mode=OneWay, Converter={StaticResource ImageConverter}}" />

        <TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Notes}" TextWrapping="Wrap" />

        <Image
            Grid.Row="1"
            Grid.Column="4"
            HorizontalAlignment="Center"
            VerticalAlignment="Top"
            Source="{Binding Path=Picture, Mode=OneWay, Converter={StaticResource PictureConverter}}" />
    </Grid>
</StackPanel>

StackPanel 方向设置为垂直",但 TextBlock 未继承它.

The StackPanel Orientation is set to 'Vertical' but the TextBlock is not inheriting it.

我哪里出错了?

推荐答案

您的问题是使用 StackPanel 允许其子项填充所有可用空间 - StackPanel> 相应地根据其内容的大小进行拉伸.尝试移除 StackPanel 并只保留 Grid - 这样您就可以将其子项的大小限制为 Grid 使用的可用空间.

Your problem is using the StackPanel that allows its children to fill in all the available space - the StackPanel stretches accordingly to the size of its content. Try removing the StackPanel and keep just the Grid - this way you will limit the size of its children to the available space used by the Grid.

如果这在您构建的布局中还不够,请尝试在需要换行的 TextBox 上设置 MaxWidth.

If that isn't enough in the layout you've built, try setting a MaxWidth on the TextBox that needs wrapping.

现在您的问题的根源还在于您的 TextBox 被插入到具有无限大小的 Grid 的第一列中 (Width="Auto").因此,将 Grid.Column="7" 设置为 TextBox 就达到了您想要的效果(换行文本).这是修改后的代码:

Now the source of your problem was also the fact that your TextBox was inserted in the first Column of the Grid that had an infinite size (Width="Auto"). Thus, setting the Grid.Column="7" to the TextBox did the trick you wanted (wrapping text). Here's the revised code:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="5" />
        <ColumnDefinition Width="15" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <DockPanel Grid.Row="0" Grid.Column="0">
        <TextBlock FontWeight="Bold" Padding="0,0,5,0" Text="{Binding Path=Id, StringFormat='#\{0\}'}" />
        <TextBlock FontWeight="Bold" Padding="0,0,5,0" Text="{Binding Path=Name}" />
    </DockPanel>
    <TextBlock Grid.Row="0" Grid.Column="4" FontWeight="Bold" Text="{Binding Path=Time, StringFormat={}{0:HH:mm}}" />
    <Image
        Grid.Row="0"
        Grid.Column="6"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Source="{Binding Path=Image, Mode=OneWay, Converter={StaticResource ImageConverter}}" />

    <TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="7" Text="{Binding Notes}" TextWrapping="Wrap" />

    <Image
        Grid.Row="1"
        Grid.Column="4"
        HorizontalAlignment="Center"
        VerticalAlignment="Top"
        Source="{Binding Path=Picture, Mode=OneWay, Converter={StaticResource PictureConverter}}" />
</Grid>

这篇关于TextBlock TextWrapping 不包含在 StackPanel 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆