【问题标题】:wpf: MahApps.Metro.SimpleChildWindow as a new windowwpf: MahApps.Metro.SimpleChildWindow 作为新窗口
【发布时间】:2015-07-28 14:30:16
【问题描述】:

我有一个带有已定义网格的现有 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 完全一样。我有两个项目并排,但看不出区别。谁能帮帮我?

谢谢!

【问题讨论】:

    标签: wpf mahapps.metro


    【解决方案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>
    

    希望有帮助!

    【讨论】:

    • 谢谢!这真的很有帮助。我选择了第一个解决方案并使用了 Grid.ColumnSpan 和 Grid.RowSpan。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 2012-10-15
    • 2016-03-23
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多