wpf: MahApps.Metro.SimpleChildWindow 作为一个新的 window

wpf: MahApps.Metro.SimpleChildWindow as a new window

我有一个带有已定义网格的现有 WPF MainWindow.xaml。在这些网格字段之一中,有一个按钮可以打开以下“ChildWindow”:

<Controls:MetroWindow   x:Class="RFM_data_analyzer.MainWindow"
                        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                        xmlns:oxy="http://oxyplot.org/wpf"
                        xmlns:local="clr-namespace:WpfApplication1"
                        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
                        xmlns:simpleChildWindow="clr-namespace:MahApps.Metro.SimpleChildWindow;assembly=MahApps.Metro.SimpleChildWindow"


                        Title="RFM data analyzer (WPF)"
                        WindowStyle="ThreeDBorderWindow" ResizeMode="CanResize"
                        WindowStartupLocation="CenterScreen" WindowState="Maximized" Height="855" Width="1024">

    <Window.DataContext>
        <local:MainViewModel/>
    </Window.DataContext>


    <Grid>
        <!-- Definition of Rows and Columns -->
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="250" />
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150"/>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="250" />
        </Grid.ColumnDefinitions>

<!-- Here are some other, uninteresing code lines -->

        <simpleChildWindow:ChildWindow x:Name="ChartControllings"
                                       CloseByEscape="False"
                                       ChildWindowWidth="400"
                                       ChildWindowHeight="300"
                                       HorizontalContentAlignment="Stretch"
                                       VerticalContentAlignment="Stretch"
                                       HorizontalAlignment="Center"
                                       VerticalAlignment="Center"
                                       Padding="1"
                                       ChildWindowImage="Error"
                                       Title="Chart Controllings">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="0"
                           Margin="20"
                           FontSize="36"
                           FontWeight="Thin"
                           Text="awesome!" />
                <TextBox Grid.Row="1" />
                <Button Grid.Row="2"
                        Margin="20"
                        IsDefault="True"
                        VerticalAlignment="Top"
                        FontSize="20"
                        FontWeight="Thin"
                        Content="Close Me"/>
            </Grid>

        </simpleChildWindow:ChildWindow>
    </Grid>
</Controls:MetroWindow>

来源在这里:MahApps.Metro.SimpleChildWindow

如果我将代码包含在 MainWindow.xaml 的网格区域内,如上所示,window 在 Grid.Row="0" Grid.Column="0 " 默认情况下,而不是像 demo project 一样在屏幕中央成为一个独立的新 window。我有两个项目并排但看不出区别。谁能帮帮我?

谢谢!

您在根网格中定义了行和列,因此您应该用 Grid.ColumnSpanGrid.RowSpan:

对 child window 这么说
<simpleChildWindow:ChildWindow x:Name="ChartControllings"
                                   Grid.ColumnSpan="6"
                                   Grid.RowSpan="6"
                                   CloseByEscape="False"
                                   ChildWindowWidth="400"
                                   ChildWindowHeight="300"
                                   HorizontalContentAlignment="Stretch"
                                   VerticalContentAlignment="Stretch"
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center"
                                   Padding="1"
                                   ChildWindowImage="Error"
                                   Title="Chart Controllings">

或者您插入另一个 Grid 并像这样操作:

<Grid>
  <Grid>
      <!-- Definition of Rows and Columns -->
      <Grid.RowDefinitions>
          <RowDefinition Height="50" />
          <RowDefinition Height="*" />
          <RowDefinition Height="*" />
          <RowDefinition Height="*" />
          <RowDefinition Height="*" />
          <RowDefinition Height="250" />
      </Grid.RowDefinitions>

      <Grid.ColumnDefinitions>
          <ColumnDefinition Width="150"/>
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="250" />
      </Grid.ColumnDefinitions>

  <!-- Here are some other, uninteresing code lines -->

  </Grid>

  <simpleChildWindow:ChildWindow x:Name="ChartControllings"
                                 CloseByEscape="False"
                                 ChildWindowWidth="400"
                                 ChildWindowHeight="300"
                                 HorizontalContentAlignment="Stretch"
                                 VerticalContentAlignment="Stretch"
                                 HorizontalAlignment="Center"
                                 VerticalAlignment="Center"
                                 Padding="1"
                                 ChildWindowImage="Error"
                                 Title="Chart Controllings">
      <Grid>
          <Grid.RowDefinitions>
              <RowDefinition Height="*" />
              <RowDefinition Height="*" />
              <RowDefinition Height="*" />
          </Grid.RowDefinitions>
          <TextBlock Grid.Row="0"
                     Margin="20"
                     FontSize="36"
                     FontWeight="Thin"
                     Text="awesome!" />
          <TextBox Grid.Row="1" />
          <Button Grid.Row="2"
                  Margin="20"
                  IsDefault="True"
                  VerticalAlignment="Top"
                  FontSize="20"
                  FontWeight="Thin"
                  Content="Close Me"/>
      </Grid>

  </simpleChildWindow:ChildWindow>

</Grid>

希望对您有所帮助!