own3dh2so4
own3dh2so4

Reputation: 401

TextBlock TextWrapping doesn't work WP 8.1

I have this XAML code:

  <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ScrollViewer x:Name="ScrollViewer" Grid.Row="0" Background="Red">
            <StackPanel x:Name="chat" >                   
            </StackPanel>
        </ScrollViewer> 
  </Grid>

And I am adding TextBlocks into the StackPanel called "chat" with this code:

    public void ponerMensaje(string mensaje, bool me)
    {
       StackPanel panelTexto = new StackPanel();
        panelTexto.Orientation = System.Windows.Controls.Orientation.Horizontal;
        Thickness marginpanel = panelTexto.Margin;
        marginpanel.Bottom = 10;
        panelTexto.Margin = marginpanel;

        //Create the colorBrush
        SolidColorBrush yellowBrush = new SolidColorBrush();
        yellowBrush.Color = Colors.Yellow;
        SolidColorBrush blackBrush = new SolidColorBrush();
        blackBrush.Color = Colors.Black;

        //Create the triangle
        Polygon yellowTriangle = new Polygon();
        yellowTriangle.Fill = yellowBrush;
        //Create the triangle's points
        System.Windows.Point Point1 = new System.Windows.Point(0, 0);
        System.Windows.Point Point2 = new System.Windows.Point(10, 0);
        System.Windows.Point Point3;         
        if (!me)
            Point3 = new System.Windows.Point(10, 10);
        else
            Point3 = new System.Windows.Point(0, 10);
        PointCollection polygonPoints = new PointCollection();
        polygonPoints.Add(Point1);
        polygonPoints.Add(Point2);
        polygonPoints.Add(Point3);

        //Add the points
        yellowTriangle.Points = polygonPoints;


        //Create the textblock
        Grid gridParaTexto = new Grid();// In WP TextBlocks haven't Backgroundcolor
        gridParaTexto.Background = yellowBrush;
        TextBlock texto = new TextBlock();
        texto.TextWrapping = TextWrapping.Wrap;
        texto.Text = mensaje;
        texto.Foreground = blackBrush;
        gridParaTexto.Children.Add(texto);            

        //Add the message
        if (!me)
        {

            panelTexto.Children.Add(yellowTriangle);
            panelTexto.Children.Add(gridParaTexto);
            chat.Children.Add(panelTexto);
        }
        else
        {
            panelTexto.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            panelTexto.Children.Add(gridParaTexto);
            panelTexto.Children.Add(yellowTriangle);
            chat.Children.Add(panelTexto);

        }
    }

This code works, but the Textblock.TextWrapping not. I'm new in WP maybe this code isn't the best, if you see other error, let me.

Upvotes: 0

Views: 363

Answers (1)

Łukasz Rejman
Łukasz Rejman

Reputation: 1892

This line is the reason:

panelTexto.Orientation = System.Windows.Controls.Orientation.Horizontal;

When you set orientation of the StackPanel to horizontal, then it will be as wide as its content. You have to change your layout, for example use Grid instead.

I have modified your code. This should work as you described:

    public void ponerMensaje(string mensaje, bool me)
    {
        Grid panelTexto = new Grid();
        Thickness marginpanel = panelTexto.Margin;
        marginpanel.Bottom = 10;
        panelTexto.Margin = marginpanel;

        //Create the colorBrush
        SolidColorBrush yellowBrush = new SolidColorBrush();
        yellowBrush.Color = Colors.Yellow;
        SolidColorBrush blackBrush = new SolidColorBrush();
        blackBrush.Color = Colors.Black;

        //Create the triangle
        Polygon yellowTriangle = new Polygon();
        yellowTriangle.Fill = yellowBrush;
        //Create the triangle's points
        System.Windows.Point Point1 = new System.Windows.Point(0, 0);
        System.Windows.Point Point2 = new System.Windows.Point(10, 0);
        System.Windows.Point Point3;
        if (!me)
            Point3 = new System.Windows.Point(10, 10);
        else
            Point3 = new System.Windows.Point(0, 10);
        PointCollection polygonPoints = new PointCollection();
        polygonPoints.Add(Point1);
        polygonPoints.Add(Point2);
        polygonPoints.Add(Point3);

        //Add the points
        yellowTriangle.Points = polygonPoints;


        //Create the textblock
        Grid gridParaTexto = new Grid();// In WP TextBlocks haven't Backgroundcolor
        gridParaTexto.Background = yellowBrush;
        TextBlock texto = new TextBlock();
        texto.TextWrapping = TextWrapping.Wrap;
        texto.Text = mensaje;
        texto.Foreground = blackBrush;
        gridParaTexto.Children.Add(texto);

        panelTexto.Children.Add(yellowTriangle);
        panelTexto.Children.Add(gridParaTexto);

        //Add the message
        if (!me)
        {
            panelTexto.ColumnDefinitions.Add(new ColumnDefinition()
            {
                Width = GridLength.Auto,
            });
            panelTexto.ColumnDefinitions.Add(new ColumnDefinition()
            {
                Width = new GridLength(1, GridUnitType.Star),
            });

            Grid.SetColumn(gridParaTexto, 1);
            Grid.SetColumn(yellowTriangle, 0);

            chat.Children.Add(panelTexto);
        }
        else
        {
            panelTexto.ColumnDefinitions.Add(new ColumnDefinition()
            {
                Width = new GridLength(1, GridUnitType.Star),
            });
            panelTexto.ColumnDefinitions.Add(new ColumnDefinition()
            {
                Width = GridLength.Auto,
            });

            gridParaTexto.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            Grid.SetColumn(gridParaTexto, 0);
            Grid.SetColumn(yellowTriangle, 1);

            chat.Children.Add(panelTexto);

        }
    }

I have replaced StackPanel with Grid with two columns. One column is for yellow triangle and another one is for text.

Upvotes: 5

Related Questions