[英]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添加到稱為“聊天”的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
。 一欄代表黃色三角形,另一欄代表文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.