在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();
}
}
在上述示例中,主窗體中有一個按鈕,當點擊按鈕時,會將子窗體顯示為模態對話框。