Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
953 views
in Technique[技术] by (71.8m points)

wpf - TextBlock TextWrapping not wrapping inside StackPanel

I have a StackPanel, but the following line :

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

is not Wrapping the Text.

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

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

Where am I going wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...