0
votes

I got a weird problem with the rowSpan property of a grid.

I got a text that will take more than one line, so I told his stack spanel, RowSpan="2", so the text can be on multiple line, using Wrapping to, like this :

<StackPanel Grid.Row="0"
            Grid.RowSpan="2"
            Orientation="Horizontal">
    <TextBlock x:Name="tbConfirm" 
               Text={Binding TextConfirm} 
               HorizontalAlignment="Center" 
               TextWrapping="Wrap"
               VerticalAlignment="Center" 
               Grid.RowSpan="2"
               FontSize="12">
    </TextBlock>
</StackPanel>

The property TextConfirm contains a text that should take two lines (I tried with the text without binding to see if it fills.

But, despite my RowSpan and my TextWrapping, it still on one line, and don't understand why.

When I make a column span, it will take the number of column define, but why row span doesn't work?

There is in the Grid, 5 column(width auto) and 5 row(height auto) define.

If I want to have one textblock, one textbox, and another one textblock.

How can I tell to go in the new line automatically? Undefined width for the two textblocks, and define width for the textbox.

This is just an example, I don't wrote it at the moment.

    <StackPanel Grid.Row="0"
                Grid.RowSpan="2"
                Orientation="Horizontal">
        <TextBlock x:Name="tbConfirm" 
                   Text={Binding TextConfirm} 
                   HorizontalAlignment="Center" 
                   TextWrapping="Wrap"
                   VerticalAlignment="Center" 
                   Grid.RowSpan="2"
                   FontSize="12">
        </TextBlock>        

<textBox width ="200"/>
<TextBlock x:Name="tbConfirm2" 
                   Text={Binding TextConfirm2} 
                   HorizontalAlignment="Center" 
                   TextWrapping="Wrap"
                   VerticalAlignment="Center" 
                   Grid.RowSpan="2"
                   FontSize="12">
        </TextBlock>
    </StackPanel>

Will this, go automatically at the new line if necessary? Or do I need to define lines and colums in the xaml?

Thank you.

2
It might be to do with the Width of the TextBlock.BenCr
You mean, it doesn't inherit on the int's containers widht for the maximum width? Indeed, I must define it. But so, I do not need anymore to set a RowSpan if the MaxWidh and the Wrapping are defines right?provençal le breton
I'm not sure what you're asking, you don't need a RowSpan to wrap text, the row height will just grow depending on it's content/height value.BenCr
Ok. But this works only with one element ? Adding a case in the question.provençal le breton
I've created a sample project here which shows text wrapping in action. github.com/B3nCr/StackOverflow_WPF_Sample Text wrapping is very simple, as @dmusial explains below so it sounds like your parent element is wider than you think it is. This means your text isn't reaching the right hand side and wrapping, try putting a thick border around the stack panel and seeing where the edges are. You could also try silverlight spy and see if that helps. firstfloorsoftware.com/silverlightspyBenCr

2 Answers

0
votes

I'm not sure if i understand the question correctly but to get the text wrapping you don't need to span accross multiple rows. From what i've read you have set the column width to auto so it will let the TextBlock "grow" freely. If you want to get the wrapping working you should define a fixed width either on the column or on the TextBlock itself (and keep the TextWrapping="Wrap").

-1
votes

try binding the width of the textblock to that of the parent as you wont want it to exceed that

<TextBlock x:Name="tbConfirm" 
           Text="asasddadsa adassdasd asdasdasdsa asdasdssd asdasdasjdahsjakdhksajdhjsaalskasj skjdsajkdhajhasaskjhdsahsakdasjdhsajdhasjdhkjsad" 
           HorizontalAlignment="Center"
                   Foreground="Black"

                   Width="{Binding ActualWidth, ElementName=stackPanelParent}"
           TextWrapping="Wrap"
           VerticalAlignment="Center" 
           FontSize="12">

        </TextBlock>

I gave my own content to test. In case it overflows let me know.