对齐表格列/段落中的文本
TableColumn 中的文本可以垂直对齐在中间吗?
我将段落添加到表格的表格单元格中。我将这个表添加到 richTexBox 控件中。 问题是中间列由图像(表情符号)组成,并且它“与垂直中心对齐”。
我很难描述它。
这是一个图像:
我想将第一列和第三列垂直对齐在中间。我希望这 3 列中的文本位于同一“级别:。
用这 3 列创建表格的功能在这里:
public Table ConvertToTabRp(IRp rp, string avatarNick)
{
var tempRp = new Rp { RpText = rp.RpText, Nick = rp.Nick, Time = rp.Time, Your = rp.Your };
if (tempRp.Your)
tempRp.Nick = avatarNick;
string time = string.Format(CultureInfo.CurrentCulture, "{0}", DateTime.ParseExact(tempRp.Time, "yyyy-MM-dd HH:mm:ss", null)
.ToString("dd-MM-yyyy HH:mm:ss").Replace("-", "."));
var tab = new Table {Margin = new Thickness(0, 0, 0, 0)};
var gridLenghtConvertor = new GridLengthConverter();
//1. col
tab.Columns.Add(new TableColumn { Name = "colNick", Width = (GridLength)gridLenghtConvertor.ConvertFromString("100") });
//2.col
tab.Columns.Add(new TableColumn { Name = "colMsg", Width = (GridLength)gridLenghtConvertor.ConvertFromString("Auto") });
//3.col
tab.Columns.Add(new TableColumn { Name = "colDt", Width = (GridLength)gridLenghtConvertor.ConvertFromString("150") });
tab.RowGroups.Add(new TableRowGroup());
tab.RowGroups[0].Rows.Add(new TableRow());
var tabRow = tab.RowGroups[0].Rows[0];
//1.col - NICK
var pNick = new Paragraph(new LineBreak()) { TextAlignment = TextAlignment.Left };
pNick.Inlines.Add(new Run(tempRp.Nick));
if (!tempRp.Your)
{
pNick.Foreground = Brushes.DodgerBlue;
}
tabRow.Cells.Add(new TableCell(pNick));
//2.col - MESSAGE
tabRow.Cells.Add(new TableCell(ConvertToRpWithEmoticons(tempRp.RpText)) { TextAlignment = TextAlignment.Left});
//3.col - DATE TIME
var pTime = new Paragraph(new LineBreak()) { TextAlignment = TextAlignment.Right };
pTime.Inlines.Add(time);
tabRow.Cells.Add(new TableCell(pTime));
return tab;
}
也许解决方案可以是:
var pNick = new Paragraph(new LineBreak()) { TextAlignment = TextAlignment.Left , **VerticalTextAligment = Center** };
我不知道段落中如何垂直对齐。
编辑:
也许我如何将图像添加到段落中的方式有问题,这是代码:
var image = new Image
{
Source = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute)),
Width = 20,
Height = 20,
};
para.Inlines.Add(new Run(separatemsg.Text) { BaselineAlignment = BaselineAlignment.TextBottom });
//insert smile
para.Inlines.Add(new InlineUIContainer(image) {BaselineAlignment = BaselineAlignment.Bottom});
并且该段落添加到 2. 列。
It possible vertical align text in TableColumn on middle?
I add paragraph to the table cell of table. And this table I add to richTexBox control.
Problem is that middle column consists of images(emoticons) and it is "align to the vertical center".
It’s difficult for me to describe it.
Here is an image:
I would like align first and third column vertical on the middle. I want text in this 3 columns on the same "level:.
Func which create Table with this 3 columns is here:
public Table ConvertToTabRp(IRp rp, string avatarNick)
{
var tempRp = new Rp { RpText = rp.RpText, Nick = rp.Nick, Time = rp.Time, Your = rp.Your };
if (tempRp.Your)
tempRp.Nick = avatarNick;
string time = string.Format(CultureInfo.CurrentCulture, "{0}", DateTime.ParseExact(tempRp.Time, "yyyy-MM-dd HH:mm:ss", null)
.ToString("dd-MM-yyyy HH:mm:ss").Replace("-", "."));
var tab = new Table {Margin = new Thickness(0, 0, 0, 0)};
var gridLenghtConvertor = new GridLengthConverter();
//1. col
tab.Columns.Add(new TableColumn { Name = "colNick", Width = (GridLength)gridLenghtConvertor.ConvertFromString("100") });
//2.col
tab.Columns.Add(new TableColumn { Name = "colMsg", Width = (GridLength)gridLenghtConvertor.ConvertFromString("Auto") });
//3.col
tab.Columns.Add(new TableColumn { Name = "colDt", Width = (GridLength)gridLenghtConvertor.ConvertFromString("150") });
tab.RowGroups.Add(new TableRowGroup());
tab.RowGroups[0].Rows.Add(new TableRow());
var tabRow = tab.RowGroups[0].Rows[0];
//1.col - NICK
var pNick = new Paragraph(new LineBreak()) { TextAlignment = TextAlignment.Left };
pNick.Inlines.Add(new Run(tempRp.Nick));
if (!tempRp.Your)
{
pNick.Foreground = Brushes.DodgerBlue;
}
tabRow.Cells.Add(new TableCell(pNick));
//2.col - MESSAGE
tabRow.Cells.Add(new TableCell(ConvertToRpWithEmoticons(tempRp.RpText)) { TextAlignment = TextAlignment.Left});
//3.col - DATE TIME
var pTime = new Paragraph(new LineBreak()) { TextAlignment = TextAlignment.Right };
pTime.Inlines.Add(time);
tabRow.Cells.Add(new TableCell(pTime));
return tab;
}
maybe solution can be:
var pNick = new Paragraph(new LineBreak()) { TextAlignment = TextAlignment.Left , **VerticalTextAligment = Center** };
I don’t how vertical align in paragraph.
EDIT:
Maybe is problem in way how I add image to paragraph, here is it code:
var image = new Image
{
Source = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute)),
Width = 20,
Height = 20,
};
para.Inlines.Add(new Run(separatemsg.Text) { BaselineAlignment = BaselineAlignment.TextBottom });
//insert smile
para.Inlines.Add(new InlineUIContainer(image) {BaselineAlignment = BaselineAlignment.Bottom});
and this paragraph is add to 2. column.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 BaselineAlignment 属性。
使用 Run-Inline 放置文本进入段落并设置 BaselineAlignment 属性。
您也可以将其用于图像。为此,请使用 InlineUIContainer 的 BaselineAlignment-property
Try use the BaselineAlignment-property.
Use a Run-Inline to put your text into the paragraphs and set the BaselineAlignment-property.
You can use this also for the image. Use for this the BaselineAlignment-property of the InlineUIContainer