NoWar
NoWar

Reputation: 37642

How to setup correct wrap style for a TextBlock?

I need to have complete wraped text to WPF Textblock. The condition is that we don't know the text length as well as window size.

The XAML I have is:

<Border BorderThickness="1" CornerRadius="6" BorderBrush="#FF405DEF" Padding="5" Margin="5">
        <StackPanel Orientation="Horizontal">
            <Image  HorizontalAlignment="Left" Source="{Binding Path=User.ProfileImageUrl}"  Width="48" Height="48"
                    Name="UserAvatarImage" Stretch="Fill" VerticalAlignment="Top"  />
            <StackPanel Margin="5,0,0,5" Orientation="Vertical" Grid.Column="1"  HorizontalAlignment="Left" Name="stackPanel1" VerticalAlignment="Top" Width="413">
                <StackPanel Orientation="Horizontal" >
                    <StackPanel Orientation="Horizontal" >
                        <TextBlock Name="UserNameTextBlock"  Margin="0,0,10,0"  Text="{Binding Path=User.Name}" FontWeight="Bold">UserName</TextBlock>
                        <TextBlock   Foreground="LightGray" >@</TextBlock>
                        <TextBlock   Name="ScreenNameTextBlock"  Text="{Binding Path=User.ScreenName}" Foreground="Gray" >ScreenName</TextBlock>
                    </StackPanel>
                </StackPanel>
                <StackPanel Orientation="Horizontal"  Margin="0,5,0,0">
                    <Grid >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="2*" />
                            <ColumnDefinition Width="2*" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Margin="0,0,0,0" Foreground="Blue" TextWrapping="Wrap">@ToUserName</TextBlock>
                        <StackPanel Grid.Column="1">
                            <TextBlock Margin="5,0,0,0" Text="{Binding Path=Text}" MinHeight="20"  MinWidth="200" 
                                       VerticalAlignment="Stretch" TextWrapping="WrapWithOverflow">START skf skdjf skdj hfskdjf ksdjhf ksjdhf ksjhf kjsf kjshf kjshkjfhsdf kjsfdkj hskdfj hskdjf hskdjf skjhfksjfks END</TextBlock>
                        </StackPanel>    
                    </Grid>
                </StackPanel>
            </StackPanel>
        </StackPanel>
    </Border>

So I need this: enter image description here

It is done by hardcoded value Width="300"

Upvotes: 0

Views: 891

Answers (1)

Rachel
Rachel

Reputation: 132618

I'm assuming this is related to your other question about binding the MaxWidth of one control to the ActualWidth of another control

In your situation, I'd replace the Horizontal StackPanel with a Grid because a Grid will limit the size of it's children to the available space, while a StackPanel will not. This way the TextBlock won't need the MaxWidth property set to wrap correctly.

Upvotes: 1

Related Questions