在MainWindow上,我尝试加载UserControl1并清除Grid,但“清除”无效。
我在UserControl1上有一个网格,如:
<Grid>
<Button Click="ButtonBase_OnClick">Clear Grid Button</Button>
</Grid>
UserControl的代码后面:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
MainWindow main=new MainWindow();
main.ClearGrid();
}
然后在MainWindow上,我们尝试将此userControl添加到Grid并清除它。
<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>
MainWindow的代码后面:
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
main.Children.Add(new UserControl1());
}
public void ClearGrid()
{
main.Children.Clear();
}