我在ListView中的ListViewItem中有一个RichTextBlock。我不能为我的生活找到为什么RichTextBlock上的文本包装不适用。 XAML:
<ScrollViewer x:Name="MessagesScroller" HorizontalScrollMode="Disabled">
<ListView x:Name="Messages" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/>
</ScrollViewer>
C#
ListViewItem listviewitem = new ListViewItem();
listviewitem.HorizontalContentAlignment = HorizontalAlignment.Stretch;
listviewitem.VerticalAlignment = VerticalAlignment.Stretch;
listviewitem.Tag = message.Id;
StackPanel stack = new StackPanel();
stack.Orientation = Orientation.Horizontal;
stack.VerticalAlignment = VerticalAlignment.Stretch;
Image Avatar = new Image();
Avatar.Height = 50;
Avatar.VerticalAlignment = VerticalAlignment.Top;
Avatar.Source = new BitmapImage(new Uri("https://cdn.discordapp.com/avatars/" + message.User.Id + "/" + message.User.Avatar + ".jpg"));
stack.Children.Add(Avatar);
StackPanel innerstack = new StackPanel();
innerstack.VerticalAlignment = VerticalAlignment.Stretch;
StackPanel MsgData = new StackPanel();
MsgData.Orientation = Orientation.Horizontal;
#region RichTextBlock
RichTextBlock user = new RichTextBlock();
user.TextWrapping = TextWrapping.WrapWholeWords;
Paragraph userPara = new Paragraph();
Run run1 = new Run();
run1.Text = message.User.Username;
userPara.Inlines.Add(run1);
user.Blocks.Add(userPara);
#endregion
MsgData.Children.Add(user);
#region RichTextBlock
RichTextBlock timestamp = new RichTextBlock();
Paragraph timePara = new Paragraph();
Run run2 = new Run();
run2.Text = message.Timestamp.Month.ToString() + "/" + message.Timestamp.Day + " at " + message.Timestamp.Hour.ToString() + ":";
if (message.Timestamp.Minute < 9)
{
run2.Text += "0";
}
run2.Text += message.Timestamp.Minute.ToString();
timestamp.Foreground = GetSolidColorBrush("#FF333333");
timePara.Inlines.Add(run2);
timestamp.Blocks.Add(timePara);
timestamp.Margin = new Thickness(5, 0, 0, 0);
#endregion
MsgData.Children.Add(timestamp);
innerstack.Children.Add(MsgData);
#region RichTextBlock
RichTextBlock txtblock = new RichTextBlock();
txtblock.TextWrapping = TextWrapping.WrapWholeWords;
Paragraph txtPara = new Paragraph();
Run run3 = new Run();
run3.Text = message.Content;
txtPara.Inlines.Add(run3);
txtblock.Blocks.Add(txtPara);
foreach (SharedModels.Embed embed in message.Embeds)
{
Paragraph paragraph = new Paragraph();
if (embed.title != null)
{
Run title = new Run();
title.Text = embed.title + "\n";
paragraph.Inlines.Add(title);
}
if (embed.Description != null)
{
Run desc = new Run();
desc.Text = embed.Description + "\n";
paragraph.Inlines.Add(desc);
}
if (embed.Thumbnail.Url != null)
{
InlineUIContainer container = new InlineUIContainer();
BitmapImage bi = new BitmapImage(new Uri(embed.Thumbnail.ProxyUrl));
Image image = new Image();
image.Height = 300;
image.Source = bi;
container.Child = image;
paragraph.Inlines.Add(container);
}
txtblock.Blocks.Add(paragraph);
}
foreach (SharedModels.Attachment attach in message.Attachments)
{
Paragraph paragraph = new Paragraph();
Run run = new Run();
run.Text = attach.Filename;
BitmapImage bi = new BitmapImage(new Uri(attach.Url));
Image image = new Image();
image.Height = 300;
image.Source = bi;
InlineUIContainer container = new InlineUIContainer();
container.Child = image;
paragraph.Inlines.Add(run);
paragraph.Inlines.Add(container);
txtblock.Blocks.Add(paragraph);
}
#endregion
innerstack.Children.Add(txtblock);
stack.Children.Add(innerstack);
listviewitem.Content = stack;
#region Flyout
Flyout flyout = new Flyout();
StackPanel flyoutcontent = new StackPanel();
flyoutcontent.Margin = new Thickness(-10);
/*Button AddRection = new Button()
{
Content = "Add Reaction",
HorizontalAlignment = HorizontalAlignment.Stretch
};
flyoutcontent.Children.Add(AddRection);
Button Pin = new Button()
{
Content = "Pin",
HorizontalAlignment = HorizontalAlignment.Stretch
};
flyoutcontent.Children.Add(Pin);*/
/*Button Edit = new Button()
{
Content = "Edit",
HorizontalAlignment = HorizontalAlignment.Stretch,
Tag = message.Id
};
Edit.Click += EditMessage;
flyoutcontent.Children.Add(Edit);*/
Button Delete = new Button()
{
Content = "Delete",
HorizontalAlignment = HorizontalAlignment.Stretch,
Tag = message.Id
};
Delete.Click += DeleteThisMessage;
flyoutcontent.Children.Add(Delete);
flyout.Content = flyoutcontent;
listviewitem.ContextFlyout = flyout;
listviewitem.RightTapped += OpenRightTapFlyout;
listviewitem.Holding += OpenHoldingFlyout;
#endregion
Messages.Items.Add(listviewitem);
我已将我的整个程序连根拔起以使用StackPanels,但这没有帮助
答案 0 :(得分:1)
您使用StackPanel
作为布局,并为Horizontal
将Orientation
属性设置为StackPanel
。 StackPanel
的大小与其子女相同。因此,似乎原因是您没有为Width
设置RichTextBlock
属性。 width
RichTextBlock
大小为其内容,StackPanel size to
RichTextBlock`,它可能无法换行。
您可能有多种方法可以解决此问题:
为width
设置RichTextBlock
属性。
RichTextBlock txtblock = new RichTextBlock();
txtblock.Width = 300;
txtblock.TextWrapping = TextWrapping.WrapWholeWords;
如果您不需要这样的布局,请移除orientation
的{{1}}属性。
StackPanel
或者您可以使用Grid代替。网格可以更好地帮助您定义布局。您可以将图像和 //stack.Orientation = Orientation.Horizontal;
布局定义如下。
RichTextBlock