如何构建这个自动增长的WPF窗口?

时间:2012-05-17 20:35:53

标签: .net wpf

我正在努力解决如何在WPF中构建此窗口。

窗口有一个文本块,其内容在设计时是未知的。窗口应垂直增长以使整个文本块可见

我尝试了类似于以下的层次结构:

Window (auto height)
    Stack Panel (vertical orientation)
        Text Block
        Check Box
        Grid (for precise positioning of the buttons)
            Button 1
            Button 2

这是一个合理的等级制度吗?有没有更好的方法来构建它?

Desired Window

2 个答案:

答案 0 :(得分:3)

你在做什么应该工作正常。您需要确保在父窗口上使用SizeToContent Property。 像这样:

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

**代码隐藏“

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";
    }

答案 1 :(得分:0)

对我来说没问题,假设您希望按钮控件位于文本框下方,堆栈面板不会自动填充父控件。你有没有尝试过这种布局并遇到问题或者这是一个假设的问题?