TextBlock not wrapping in UserControl as screen width decreases

Apptacular Apps 386 Reputation points
2020-06-24T11:53:20.6+00:00

I'm trying to get my TextBlock inside a UserControl to wrap when the windows is snapped to one side/screen width is smaller but for some it won't wrap properly and keeps stretching over the screen limits, even though I set the horizontal alignment of the UserControl to the left. Does anyone know why this problem is happening and how to fix this?

<UserControl
    x:Class="MyApp.UserControls.MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyApp.UserControls"
    xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    HorizontalAlignment="Left">

    <controls:DropShadowPanel x:Name="grd">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <StackPanel Grid.Row="0" Orientation="Horizontal">
                <TextBlock x:Name="txtItem" TextWrapping="Wrap"/>
            </StackPanel>
            <StackPanel Grid.Row="1" Orientation="Vertical">
                <TextBlock x:Name="txtTitle"/>
                <TextBlock x:Name="txtSubtitle"/>
            </StackPanel>
        </Grid>
    </controls:DropShadowPanel>
</UserControl>

MainPage

<Page
    x:Class="MyApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyApp.MainPage"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid xmlns:src="using:MyApp.UserControls">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0">
            <TextBlock x:Name="txtTitle" />
            <TextBlock x:Name="txtSubtitle""/>
        </StackPanel>
        <ScrollViewer Grid.Row="1">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>

                <StackPanel x:Name="myStackPanel" HorizontalAlignment="Left"/>
            </Grid>
        </ScrollViewer>
    </Grid>
</Page>
Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Daniele 1,996 Reputation points
    2020-06-24T20:02:30.413+00:00

    ColumnDefinition at line 25 in MainPage must be *

    <Page
         x:Class="MyApp.MainPage"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="using:MyApp.MainPage"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d">
    
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0">
                <TextBlock x:Name="txtTitle" />
                <TextBlock x:Name="txtSubtitle"/>
            </StackPanel>
            <ScrollViewer Grid.Row="1">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
    
                    <StackPanel x:Name="myStackPanel" HorizontalAlignment="Left"/>
                </Grid>
            </ScrollViewer>
        </Grid>
     </Page>
    

    Orientation of StackPanel at line 12 must be Vertical

    <UserControl [...]>
         <controls:DropShadowPanel x:Name="grd">
             <Grid>
                 <Grid.RowDefinitions>
                     <RowDefinition Height="Auto"/>
                     <RowDefinition Height="Auto"/>
                 </Grid.RowDefinitions>
    
                 <StackPanel Grid.Row="0" Orientation="Vertical">
                     <TextBlock x:Name="txtItem" TextWrapping="Wrap"/>
                 </StackPanel>
                 <StackPanel Grid.Row="1" Orientation="Vertical">
                     <TextBlock x:Name="txtTitle"/>
                     <TextBlock x:Name="txtSubtitle"/>
                 </StackPanel>
             </Grid>
         </controls:DropShadowPanel>
     </UserControl>
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.