1

我的项目中有 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>

我被称为 Var 的 Control_Page。然后像孩子一样尝试主窗口,但它不起作用。

我试过这个不能解决这个问题。

后面的代码;

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);
    }
}
4

1 回答 1

0

插入一个窗口作为另一个窗口的子窗口是不可能的,您最终会遇到 {"Window must be the root of the tree. Cannot add Window as a child of Visual."} 异常。

您可以将 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>
于 2016-05-31T07:57:21.983 回答