「直接在XAML中」是指:將您的子窗口放入您的根網格中。
<Controls:MetroWindow x:Class="MahApps.Metro.SimpleChildWindow.Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:simpleChildWindow="clr-namespace:MahApps.Metro.SimpleChildWindow;assembly=MahApps.Metro.SimpleChildWindow"
Title="MahApps.Metro Simple ChildWindow Demo"
GlowBrush="{DynamicResource AccentColorBrush}"
WindowStartupLocation="CenterScreen">
<Grid x:Name="RootGrid">
<Grid>
<!-- main content here -->
</Grid>
<simpleChildWindow:ChildWindow x:Name="child01"
CloseByEscape="False"
Closing="Child01_OnClosing"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
Padding="15"
ChildWindowImage="Error"
Title="TestChild 1">
<Grid>
<!-- child content here -->
</Grid>
</simpleChildWindow:ChildWindow>
<simpleChildWindow:ChildWindow x:Name="child02"
ChildWindowWidth="400"
ChildWindowHeight="300"
EnableDropShadow="False"
Title="TestChild 2">
<Grid>
<!-- child content here -->
</Grid>
</simpleChildWindow:ChildWindow>
</Grid>
</Controls:MetroWindow>
如果你喜歡使用後面的代碼,那麼你可以創建一個像CustomChildWindow
定製ChildWindow以及創建和調用它
private async void OpenCustomChildWindow_OnClick(object sender, RoutedEventArgs e)
{
await this.ShowChildWindowAsync(new CustomChildWindow() { IsModal = false }, RootGrid);
// or
//await this.ShowChildWindowAsync(new CustomChildWindow() { IsModal = false }, OverlayFillBehavior.WindowContent);
// or
//await this.ShowChildWindowAsync(new CustomChildWindow() { IsModal = true }, OverlayFillBehavior.FullWindow);
}
您可以在GitHub上的主要演示發現,這也。
希望這會有所幫助。
謝謝你的例子! :) – Oleksii