我有这个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添加到StackPanel中,名为&#34; chat&#34;使用此代码:
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的新手,也许这段代码不是最好的,如果你看到其他错误,请允许我。
答案 0 :(得分:5)
这一行是原因:
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);
}
}
我已将StackPanel
替换为Grid
两列。一列用于黄色三角形,另一列用于文本。