wpf:MahApps.Metro.SimpleChildWindow作为一个新窗口

时间:2015-07-28 14:30:17

标签: wpf mahapps.metro

我有一个已定义网格的现有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的网格区域中,如上所示,窗口将在Grid.Row =“0”中打开Grid.Column =“0”默认情况下不是一个独立的新窗口屏幕中心与demo project完全一样。我有两个项目并排,但看不出差异。任何人都可以帮助我吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

您在根网格中定义了行和列,因此您应该使用Grid.ColumnSpanGrid.RowSpan向子窗口说明这一点:

<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>

希望有所帮助!