Nickolodeon
Nickolodeon

Reputation: 2956

textblock in user control TextWrapping not wrapping

I created user control with the textblock. But it will not wrap. This user control servers as a listboxitem.

 <Grid x:Name="MainGrid" Height="Auto" Width="Auto">

    <StackPanel Orientation="Horizontal">


        <Image Height="50" Width="100" Stretch="Uniform" Name="image1"  Source="{Binding Path=VideoImageUrl}" Margin="12,12,13,84" MouseLeftButtonDown="image1_MouseLeftButtonDown" MouseEnter="image1_MouseEnter" MouseLeave="image1_MouseLeave" />

            <StackPanel Orientation="Vertical" >


            <TextBlock TextWrapping="Wrap"  Height="Auto" HorizontalAlignment="Left"  Name="titleTextBox" 
                    Text="{Binding Path=Title, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" 
                    VerticalAlignment="Center" Width="Auto" FontSize="14" FontWeight="SemiBold" />
            <StackPanel Orientation="Vertical" x:Name="StackPanelDetails">

                    <TextBlock  Height="Auto" HorizontalAlignment="Left" Name="desciptionTextBox" 
                                TextWrapping="Wrap"
                    Text="{Binding Path=Desciption, Mode=OneWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" 
                    VerticalAlignment="Center" Width="Auto" />

                <Line />
                <ItemsControl x:Name="CustomItemsSource" ItemsSource="{Binding Path=LinksList}" >

                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock>
                            <Hyperlink  NavigateUri="{Binding Path=TopicUrl}" RequestNavigate="Hyperlink_RequestNavigate" >
                              <TextBlock Text="{Binding Path=TopicName}" />
                            </Hyperlink>
                        </TextBlock>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>

            </ItemsControl>
            </StackPanel>

        </StackPanel>

    </StackPanel>


</Grid>

Upvotes: 0

Views: 1688

Answers (1)

Clemens
Clemens

Reputation: 128098

A ListBox's default template does not automatically limit the width of its items, but instead uses a ScrollViewer, which shows a horizontal scrollbar when an item is wider than the ListBox.

You can remove the ScrollViewer by replacing the ListBox's Template:

<ListBox ...>
    <ListBox.Template>
        <ControlTemplate>
            <StackPanel IsItemsHost="True"/>
        </ControlTemplate>
    </ListBox.Template>
    ...
</ListBox>

Another important thing to note is that a StackPanel in the top-level Grid won't properly resize the contained elements. In the following simplified example the text in the TextBlock is not wrapped because the containing StackPanel simply does not resize it as you expect:

<Grid>
    <StackPanel Orientation="Horizontal">
        <Image ... />
        <Text TextWrapping="Wrap" Text=... />
    </StackPanel>
</Grid>

This way it works as you expect:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Image ... />
    <TextBlock Grid.Column="1" TextWrapping="Wrap" Text=... />
</Grid>

Upvotes: 5

Related Questions