首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF TextBlock高度

WPF TextBlock高度
EN

Stack Overflow用户
提问于 2013-11-06 00:09:54
回答 1查看 2.8K关注 0票数 1

我正在动态地添加TextBlocks,但是我在高度方面有问题。

有时文本显示为剪裁。此外,似乎应用程序为所有元素分配了相同的高度。

xaml:

代码语言:javascript
复制
<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>

守则如下:

代码语言:javascript
复制
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);

演示:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-06 00:48:47

不确定这是否是完美的解决方案,但我找到了一种方法,使sure属性不锁定到该行中的列。这方面的问题是,在第二列中放置任何一列之前,它将一直沿着第一列前进。对于WrapPanel来说,没有一种简单的方法可以水平地添加元素,而不锁定同一行中的所有高度。

这还可以通过使用样式来消除后面的代码。

下面是一个正在使用的例子

代码语言:javascript
复制
<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>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19801631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档