TextBlock TextWrapping 不适用于 WP 8.1

TextBlock TextWrapping doesn't work WP 8.1

我有这个 XAML 代码:

  <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>

我正在使用以下代码将 TextBlocks 添加到名为 "chat" 的 StackPanel 中:

    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);

        }
    }

此代码有效,但 Textblock.TextWrapping 无效。 我是 WP 的新手,也许这段代码不是最好的,如果您看到其他错误,请告诉我。

这一行是原因:

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

当您将 StackPanel 的方向设置为水平时,它将与其内容一样宽。您必须更改布局,例如改用 Grid

我已经修改了你的代码。这应该像您描述的那样工作:

    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);

        }
    }

我用两列的 Grid 替换了 StackPanel。一列用于黄色三角形,另一列用于文本。