在WPF中,可以使用以下几种方法在主窗体中显示多个子窗体:
示例代码:
<Window x:Class="MainWindow">
<Grid>
<ContentControl x:Name="contentControl" />
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
SubWindow subWindow = new SubWindow();
contentControl.Content = subWindow;
}
}
示例代码:
<Window x:Class="MainWindow">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Content="Show SubWindow" Click="ShowSubWindowButton_Click" />
<ContentControl x:Name="subWindowContainer" Grid.Row="1" Visibility="Collapsed" />
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ShowSubWindowButton_Click(object sender, RoutedEventArgs e)
{
if (subWindowContainer.Visibility == Visibility.Collapsed)
{
SubWindow subWindow = new SubWindow();
subWindowContainer.Content = subWindow;
subWindowContainer.Visibility = Visibility.Visible;
}
}
}
在上述示例中,主窗体中有一个按钮,当点击按钮时,会在Grid的第二行显示一个子窗体。
示例代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ShowSubWindowButton_Click(object sender, RoutedEventArgs e)
{
SubWindow subWindow = new SubWindow();
subWindow.ShowDialog();
}
}
在上述示例中,主窗体中有一个按钮,当点击按钮时,会将子窗体显示为模态对话框。