列定义中的另一个窗口

时间:2016-05-31 07:28:08

标签: c# wpf xaml rowdefinition columndefinition

我的项目中有2个窗口。其中一个是MainWindow.xaml其他一个是Control_Page.xaml。 我想将控制页面窗口插入ColumnDefinition和RowDefinition。这个定义在MainWindow上定义。

这是我的代码:

<Grid x:Name="Grid_Main" SnapsToDevicePixels="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="312*"/>
        <ColumnDefinition Width="353*"/>
        <ColumnDefinition Width="351*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="187*"/>
        <RowDefinition Height="187*"/>
        <RowDefinition Height="186*"/>
        <RowDefinition Height="178*"/>
    </Grid.RowDefinitions>

    <Grid Grid.Column="0" Grid.Row="0" Margin="0"/>
    <Grid Grid.Column="1" Margin="0"/>
    <Grid Grid.Column="2" Margin="0"/>
    <Grid Margin="0" Grid.Row="1"/>
    <Grid Grid.Column="1" Margin="0" Grid.Row="1">
        <Button Content="Button" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
    <Grid Grid.Column="2" Margin="0" Grid.Row="1">
        <Button x:Name="button1" Content="Button" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center"/>
    </Grid>
    <Grid Margin="0" Grid.Row="2"/>
    <Grid Grid.Column="1" Margin="0" Grid.Row="2"/>
    <Grid Grid.Column="2" Margin="0" Grid.Row="2"/>
    <Grid Margin="0" Grid.Row="3"/>
    <Grid Grid.Column="1" Margin="0" Grid.Row="3"/>
    <Grid Grid.Column="2" Margin="0" Grid.Row="3"/>
</Grid>

我被称为Control_Page为Var。然后尝试儿童到主窗口,但它不起作用。

我试图解决这个问题。

代码背后;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var newWindow = new Control_Page();
        Grid.SetRow(newWindow, 0);
        Grid.SetColumn(newWindow, 0);
        Grid_Main.Children.Add(newWindow);
    }
}

1 个答案:

答案 0 :(得分:0)

不能将窗口作为另一个窗口的子窗口插入,最终会得到{&#34;窗口必须是树的根。无法将Window添加为Visual的子项。&#34;} exception。

您可以将Control_Page.xaml作为UserControl添加到网格中。因此,请确保在.xaml文件(以及相应的.cs文件)中更改为从Window到UserControl的类型

<Grid x:Name="Grid_Main" SnapsToDevicePixels="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="312*"/>
        <ColumnDefinition Width="353*"/>
        <ColumnDefinition Width="351*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="187*"/>
        <RowDefinition Height="187*"/>
        <RowDefinition Height="186*"/>
        <RowDefinition Height="178*"/>
    </Grid.RowDefinitions>

    <Control_Page Grid.Column="0" Grid.Row="0" />

    <Grid Grid.Column="0" Grid.Row="0" Margin="0"/>
    <Grid Grid.Column="1" Margin="0"/>
    <Grid Grid.Column="2" Margin="0"/>
    <Grid Margin="0" Grid.Row="1"/>
    <Grid Grid.Column="1" Margin="0" Grid.Row="1">
        <Button Content="Button" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
    <Grid Grid.Column="2" Margin="0" Grid.Row="1">
        <Button x:Name="button1" Content="Button" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center"/>
    </Grid>
    <Grid Margin="0" Grid.Row="2"/>
    <Grid Grid.Column="1" Margin="0" Grid.Row="2"/>
    <Grid Grid.Column="2" Margin="0" Grid.Row="2"/>
    <Grid Margin="0" Grid.Row="3"/>
    <Grid Grid.Column="1" Margin="0" Grid.Row="3"/>
    <Grid Grid.Column="2" Margin="0" Grid.Row="3"/>
</Grid>