0

I have the following XAML:

<UserControl x:Class="TestWrappingWpf.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:TestWrappingWpf"
             mc:Ignorable="d" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
            <TextBlock TextWrapping="NoWrap">
                This should not wrap
            </TextBlock>
            <TextBlock Grid.Row="1" TextWrapping="Wrap">
                This should word wrap, but it doesn't and it stretches the StackPanel
            </TextBlock>
    </Grid>
</UserControl>

This renders as shown in the following image:

enter image description here

I would like the first TextBlock to set the width of the grid, and the second TextBlock should wrap according to the width set by the first TextBlock.

The result would look like this:

enter image description here

How can I make the second TextBlock wrap in response to the parent Grid's actual width?

Update 1

I can get the wrapping to work initially using this code:

<UserControl x:Class="TestWrappingWpf.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:TestWrappingWpf"
             mc:Ignorable="d" >
    <Grid x:Name="MainGrid">
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <TextBlock x:Name="FirstTextBlock" TextWrapping="NoWrap">
                This should not wrap
        </TextBlock>
        <TextBlock Grid.Row="1" TextWrapping="Wrap" MaxWidth="{Binding ElementName=MainGrid, Path=ActualWidth}">
                This should word wrap, but it doesn't and it stretches the Grid
        </TextBlock>
    </Grid>
</UserControl>

enter image description here

Unfortunately this doesn't shrink after the UI is shown. And if I expand the wrapping responds to the expansion, but then will not wrap if the UI shrinks.

Here is an example in a larger application. Initially the UI is wrapped:

enter image description here

However once it is expanded, it will no longer shrink.

enter image description here

Victor Chelaru
  • 4,491
  • 3
  • 35
  • 49

0 Answers0