当前位置: 动力学知识库 > 问答 > 编程问答 >

.net - How to build this auto-growing WPF window?

问题描述:

I'm struggling with how to build this window in WPF.

The window has a Text Block whose contents are not known at design time. The Window should grow vertically to make the entire Text Block visible

I've tried a hierarchy similar to:

Window (auto height)

Stack Panel (vertical orientation)

Text Block

Check Box

Grid (for precise positioning of the buttons)

Button 1

Button 2

Is this a reasonable hierarchy? Is there a better way to build this?

网友答案:

What you are doing should work fine. You will want to make sure that you use the SizeToContent Property on your parent Window. Something like this:

XAML

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"  Width="525" SizeToContent="Height">
    <Grid Height="auto" >
        <StackPanel Orientation="Vertical" >
            <TextBlock Name="tb1" TextAlignment="Justify" TextWrapping="Wrap" Text="" />
            <Grid Height="30">
                <CheckBox VerticalAlignment="Center" Name="Random">Random CheckBox</CheckBox>
            </Grid>
            <Grid Height="auto">
                <Button Name="button1" HorizontalAlignment="Right" Margin="0,0,80,0" Width="70" Height="30" Click="button1_Click" />
                <Button Name="Button2" HorizontalAlignment="Right"  Width="70" Height="30" Click="Button2_Click" />
            </Grid>
        </StackPanel>
    </Grid>
</Window>

**CodeBehind"

private void button1_Click(object sender, RoutedEventArgs e)
    {
        tb1.Text += "TextBlock with contentes set at run-time. " + 
                     "This text block should grow in height as necessary, " + 
                     "and push the other controls down.  The window itself " +
                     "should grow to \n\n Another paragraph\n";
    }
网友答案:

Looks ok to me, assuming you want the button controls to sit just under the text box, stack panels don't automatically fill parent controls. have you tried this layout yet and run into problems or is this a hypothetical question?

分享给朋友:
您可能感兴趣的文章:
随机阅读: