我正在动态地添加TextBlocks,但是我在高度方面有问题。
有时文本显示为剪裁。此外,似乎应用程序为所有元素分配了相同的高度。
xaml:
<ScrollViewer Grid.Column="2" x:Name="DetailInfoScroll"
Margin="25,0,50,0"
Style="{StaticResource HorizontalScrollViewerStyle}">
<VariableSizedWrapGrid Grid.Column="2" Margin="25,0,50,35"
HorizontalAlignment="Left"
VerticalAlignment="Center"
x:Name="StkText">
</VariableSizedWrapGrid>
</ScrollViewer>守则如下:
var txt = new TextBlock
{
Text = text,
TextWrapping = TextWrapping.Wrap,
TextAlignment = TextAlignment.Justify,
FontSize = 14,
Margin = new Thickness(0, 0, 25, 15),
MaxWidth = 400,
LineStackingStrategy = LineStackingStrategy.MaxHeight,
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Left
};
txt.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
var desiredSizeNew = txt.DesiredSize;
txt.Height = desiredSizeNew.Height;
StkText.Children.Add(txt);演示:

发布于 2013-11-06 00:48:47
不确定这是否是完美的解决方案,但我找到了一种方法,使sure属性不锁定到该行中的列。这方面的问题是,在第二列中放置任何一列之前,它将一直沿着第一列前进。对于WrapPanel来说,没有一种简单的方法可以水平地添加元素,而不锁定同一行中的所有高度。
这还可以通过使用样式来消除后面的代码。
下面是一个正在使用的例子
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="Width" Value="400"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="Margin" Value="10"></Setter>
</Style>
</Window.Resources>
<WrapPanel Orientation="Vertical" Width="850" Background="Black" Height="300" >
<TextBlock Text="Focus on questions about an actual problem you have faced. Include details about what you have tried and exactly what you are trying to do."/>
<TextBlock Text="Not all questions work well in our format. Avoid questions that are primarily opinion-based, or that are likely to generate discussion rather than answers."/>
<TextBlock Text="Questions that need improvement may be closed until someone fixes them."/>
<TextBlock Text="All questions are tagged with their subject areas. Each can have up to 5 tags, since a question might be related to several subjects. Click any tag to see a list of questions with that tag, or go to the tag list to browse for topics that interest you."/>
<TextBlock Text="Your reputation score goes up when others vote up your questions, answers and edits."/>
</WrapPanel>

https://stackoverflow.com/questions/19801631
复制相似问题