문제

I'm adding TextBlocks dynamically but I have problems with the heights.

Sometimes the text appear cut. In addition, seems that the application assigns the same height for all elements.

The 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>

and the code:

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);

Demo: Demo

도움이 되었습니까?

해결책

Not sure if this is the perfect solution but I found a way to make the height property not be locked to the columns in that row. The problem with this is it will go all the way down the first column before putting any in the second column. There is no easy way for the WrapPanel to add elements horizantally without it locking all the heights in the same row.

This also gets rid of your code behind by using styles.

Here is an example of it in use

<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>

enter image description here

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top