[英]WPF TextBlock Height
我正在动态添加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);
演示:
不知道这是否是完美的解决方案,但是我找到了一种使height属性不锁定到该行中各列的方法。 这样做的问题是,在将任何内容放入第二列之前,它将一直沿第一列向下移动。 如果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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.