.net 如何构建这个自动增长的WPF窗口?
我正在努力研究如何在WPF中构建这个窗口 窗口有一个文本块,其内容在设计时未知。窗口应垂直增长,以使整个文本块可见 我尝试过类似以下的层次结构:.net 如何构建这个自动增长的WPF窗口?,.net,wpf,.net,Wpf,我正在努力研究如何在WPF中构建这个窗口 窗口有一个文本块,其内容在设计时未知。窗口应垂直增长,以使整个文本块可见 我尝试过类似以下的层次结构: Window (auto height) Stack Panel (vertical orientation) Text Block Check Box Grid (for precise positioning of the buttons) Button 1
Window (auto height)
Stack Panel (vertical orientation)
Text Block
Check Box
Grid (for precise positioning of the buttons)
Button 1
Button 2
这是合理的等级制度吗?有没有更好的方法来构建它
在我看来还可以,假设您希望按钮控件位于文本框的正下方,堆栈面板不会自动填充父控件。您是否尝试过这种布局并遇到问题,或者这是一个假设性问题?在我看来没问题,假设您希望按钮控件位于文本框的正下方,堆栈面板不会自动填充父控件。您是否尝试过此布局并遇到问题,或者这是一个假设性问题?您正在做的应该可以正常工作。您需要确保在父窗口上使用。 大概是这样的: XAML
你所做的应该很好。您需要确保在父窗口上使用。 大概是这样的: XAML
快速看一眼,只要TextBlock配置为与其内容正确匹配,它就应该可以工作。您可能还需要指定最大宽度。你有没有试过这个但没用,或者你还在试着弄清楚总体布局?我会再试一次。它目前还不起作用,但如果这是一种不合理的方法,我不想在这方面花费更多的时间。这种方法看起来很简单,只要TextBlock的大小配置正确,它就可以正常工作。您可能还需要指定最大宽度。你有没有试过这个但没用,或者你还在试着弄清楚总体布局?我会再试一次。它目前还不起作用,但如果这是一种不合理的方法,我不想在这方面花费更多的时间。看来方法不错,谢谢!这有助于我根据需要修改窗口+1、回答谢谢!这有助于我根据需要修改窗口+1、回答
<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";
}