C# WPF:Can';无法清除包含UserControl的网格

C# WPF:Can';无法清除包含UserControl的网格,c#,wpf,user-controls,grid,C#,Wpf,User Controls,Grid,在主窗口上,我尝试加载UserControl1并清除网格,但清除不起作用 我在UserControl1上有一个网格,如: <Grid> <Button Click="ButtonBase_OnClick">Clear Grid Button</Button> </Grid> 在主窗口上,我们尝试将这个userControl添加到网格并清除它 <Window x:Class="WpfApplication1s.MainWindow"

在主窗口上,我尝试加载UserControl1并清除网格,但清除不起作用

我在UserControl1上有一个网格,如:

<Grid>
<Button  Click="ButtonBase_OnClick">Clear Grid Button</Button>
</Grid>
在主窗口上,我们尝试将这个userControl添加到网格并清除它

<Window x:Class="WpfApplication1s.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="MainWindow_OnLoaded">
    <Grid Name="main">

    </Grid>
</Window>
新建主窗口()
-您正在清除另一个(新)窗口的网格。尝试
this.ClearGrid()
((MainWindow)Application.Current.MainWindow).ClearGrid()或仅
((面板)父项)。子项。删除(此)
@Clemens((MainWindow)Application.Current.MainWindow)这工作正常,谢谢,请添加答案,准备接受:)更安全:
(父面板)?.Children.Remove(此)
<Window x:Class="WpfApplication1s.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="MainWindow_OnLoaded">
    <Grid Name="main">

    </Grid>
</Window>
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
main.Children.Add(new UserControl1());
}


public void ClearGrid()
{
main.Children.Clear();

}