如何删除Windows Phone 8中画布内的点击点?

时间:2013-12-17 07:35:38

标签: c# windows-phone-7 canvas windows-phone-8

我试图用手指在Windows手机画布上绘制一条线,我完成了这个但当我尝试删除/清除那条线时,它正在删除该画布中的所有元素,

ContentPanelCanvas.Children.Clear();

听到我的完整代码:

XAML

  <Canvas x:Name="ContentPanelCanvas" Grid.Row="1" Background="Transparent"  Margin="12,0,12,0">

        <Grid Width="450" Height="600">
        <Button x:Name="clickMeBtn" Height="72"
                Width="200"
                Content="Click Me" 
                HorizontalAlignment="Center"
                VerticalAlignment="Center" Click="clickMeBtn_Click" />
         </Grid>
   </Canvas>

C#代码是这样的:

 public MainPage()
    {
        InitializeComponent();
        this.ContentPanelCanvas.MouseMove += new MouseEventHandler(ContentPanelCanvas_MouseMove);
        this.ContentPanelCanvas.MouseLeftButtonDown += new MouseButtonEventHandler(ContentPanelCanvas_MouseLeftButtonDown);
    }

    void ContentPanelCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        currentPoint = e.GetPosition(ContentPanelCanvas);
        oldPoint = currentPoint;
    }

    void ContentPanelCanvas_MouseMove(object sender, MouseEventArgs e)
    {

            currentPoint = e.GetPosition(this.ContentPanelCanvas);

            line = new Line() { X1 = currentPoint.X, Y1 = currentPoint.Y, X2 = oldPoint.X, Y2 = oldPoint.Y };
            line.Stroke = new SolidColorBrush(Colors.White);
            line.StrokeThickness = 10;
            this.ContentPanelCanvas.Children.Add(line);
            oldPoint = currentPoint;
    }

    private void clickMeBtn_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Hello Every One ");
        ContentPanelCanvas.Children.Clear();

    }

代码执行没有错误,但它正在删除按钮,如何删除我绘制的行

3 个答案:

答案 0 :(得分:1)

嗯,代码正在完成您所写的内容,它正在清除ContentPanelCanvas的所有子代。如果您只想删除这些行,则只删除这些行:

foreach (var line in ContentPanelCanvas.Children.OfType<Line>().ToList())
    ContentPanelCanvas.Children.Remove(line);

答案 1 :(得分:1)

您可以使用

 ContentPanelCanvas.Children.Remove(Line);

或按索引删除子项

ContentPanelCanvas.Children.RemoveAt(1);

答案 2 :(得分:1)

如果清除画布,它将从UIElementCollection“Removes all elements”。

你应该创建一个带有行的事件处理程序,例如点击事件处理程序,一旦点击你应该将行控件保存在另一个控件中去除,然后在按钮点击处理程序中删除它就像这样

ContentPanelCanvas.Children.Remove(controlToDelete);

controlToDelete是您从线路点击处理程序中删除的控件,即行控制