1

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

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

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

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

这是一个合理的层次结构吗?有没有更好的方法来构建它?

所需窗口

4

2 回答 2

4

您正在做的事情应该可以正常工作。您需要确保在父窗口上使用SizeToContent 属性。像这样的东西:

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";
    }
于 2012-05-17T21:06:10.330 回答
0

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

于 2012-05-17T21:04:31.740 回答