wpf 如何清除wpf中画布的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34403009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:35:51  来源:igfitidea点击:

How to clear the contents of a canvas in wpf?

c#wpfcanvaszooming

提问by lata

How to clear the contents of a canvas in wpf? Tried canvas.clear() doesn't work. Also how can i add zoom control in a wpf application? Zoom with a scroller to move the image.

如何清除wpf中画布的内容?试过 canvas.clear() 不起作用。另外如何在 wpf 应用程序中添加缩放控件?使用滚动条缩放以移动图像。

Any help is greatly appreciated

任何帮助是极大的赞赏

回答by Kory Gill

You want to do MyCanvas.Children.Clear();

你想做 MyCanvas.Children.Clear();

Tested with the following code:

使用以下代码进行测试:

<Grid>
    <Canvas Name="MyCanvas"/>
    <Button Click="Button_Click" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="200" Content="Clear"/>
</Grid>

and

public MainWindow()
{
    InitializeComponent();
    MyCanvas.Children.Add(new TextBlock() { Text = "Foo" });
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    MyCanvas.Children.Clear();
}