如何删除一个帆布的一个孩子后,我加入了吗? [英] How to remove a single child of a single canvas after I added it?
问题描述
我有一个WPF项目中,我要创建一个画布上几个省略号。我创建了两个复选框,当我在第一个复选框选中,红色的椭圆会显示在画布上。如果我取消选中第一个复选框,椭圆将会消失....第二个复选框将创建一个蓝色的椭圆形,而不是具有相同的功能。
因此,继承人我的情况,当两个复选框被选中,蓝色和红色的椭圆会出现。要清除画布上的椭圆形,我用的是 myCanvas.children.clear()
。但是,当我取消选中该复选框中的一个,两个椭圆的将被删除。
私人无效redCB_Checked(对象发件人,RoutedEventArgs E)
{
drawRedCircle();
}
私人无效redCB_Unchecked(对象发件人,RoutedEventArgs E)
{
myCanvas.Children.Clear();
}
私人无效blueCB_Checked(对象发件人,RoutedEventArgs E)
{
drawBlueCircle();
}
私人无效blueCB_Unchecked(对象发件人,RoutedEventArgs E)
{
myCanvas.Children.Clear();
}
私人无效drawRedCircle()
{
椭圆myCircle =新的椭圆();
myCircle.Stroke = Brushes.Red;
myCircle.Width = 30;
myCircle.Height = 30;
myCircle.StrokeThickness = 2;
Canvas.SetLeft(myCircle,10);
Canvas.SetRight(myCircle,10);
Canvas.SetBottom(myCircle,10);
Canvas.SetTop(myCircle,10);
myCanvas.Children.Add(myCircle);
}
私人无效drawBlueCircle()
{
椭圆myCircle =新的椭圆();
myCircle.Stroke = Brushes.Blue;
myCircle.Width = 30;
myCircle.Height = 30;
myCircle.StrokeThickness = 2;
Canvas.SetLeft(myCircle,20);
Canvas.SetRight(myCircle,20);
Canvas.SetBottom(myCircle,20);
Canvas.SetTop(myCircle,20);
myCanvas.Children.Add(myCircle);
}
如果你给的加圈一个名字,你可以找到它时,复选框被选中,然后很容易地将其删除。
私人字符串redCircleName =redCircle ;
私人字符串blueCircleName =blueCircle;
私人无效redCB_Checked(对象发件人,RoutedEventArgs E)
{
drawRedCircle();
}
私人无效redCB_Unchecked(对象发件人,RoutedEventArgs E)
{
RemoveCircleByName(redCircleName);
}
私人无效blueCB_Checked(对象发件人,RoutedEventArgs E)
{
drawBlueCircle();
}
私人无效blueCB_Unchecked(对象发件人,RoutedEventArgs E)
{
RemoveCircleByName(blueCircleName);
}
私人无效RemoveCircleByName(字符串名称)
{
VAR圈=(的UIElement)LogicalTreeHelper.FindLogicalNode(myCanvas,名);
myCanvas.Children.Remove(圈);
}
私人无效drawRedCircle()
{
椭圆myCircle =新的椭圆();
myCircle.Stroke = Brushes.Red;
myCircle.Width = 30;
myCircle.Height = 30;
myCircle.StrokeThickness = 2;
//给它一个名字在这里,所以我们以后可以
myCircle.Name = redCircleName找到它;
Canvas.SetLeft(myCircle,10);
Canvas.SetRight(myCircle,10);
Canvas.SetBottom(myCircle,10);
Canvas.SetTop(myCircle,10);
myCanvas.Children.Add(myCircle);
}
私人无效drawBlueCircle()
{
椭圆myCircle =新的椭圆();
myCircle.Stroke = Brushes.Blue;
myCircle.Width = 30;
myCircle.Height = 30;
myCircle.StrokeThickness = 2;
//给它一个名字在这里,所以我们以后可以
myCircle.Name = blueCircleName找到它;
Canvas.SetLeft(myCircle,20);
Canvas.SetRight(myCircle,20);
Canvas.SetBottom(myCircle,20);
Canvas.SetTop(myCircle,20);
myCanvas.Children.Add(myCircle);
}
I have a WPF project where i have to create a few ellipses on a canvas. I create two check Box and when i check on the first checkbox, the red ellipse will show on the canvas. If i uncheck the first check box, the ellipse will be disappear....the second check box will have the same function by creating a blue ellipse instead.
So heres my situation, when the two checkboxes are checked, a blue and a red ellipse will appear. To clear the ellipse on the canvas, i use the myCanvas.children.clear()
. But when i uncheck one of the checkbox, both of the ellipse will be deleted.
private void redCB_Checked(object sender, RoutedEventArgs e)
{
drawRedCircle();
}
private void redCB_Unchecked(object sender, RoutedEventArgs e)
{
myCanvas.Children.Clear();
}
private void blueCB_Checked(object sender, RoutedEventArgs e)
{
drawBlueCircle();
}
private void blueCB_Unchecked(object sender, RoutedEventArgs e)
{
myCanvas.Children.Clear();
}
private void drawRedCircle()
{
Ellipse myCircle = new Ellipse();
myCircle.Stroke = Brushes.Red;
myCircle.Width = 30;
myCircle.Height = 30;
myCircle.StrokeThickness = 2;
Canvas.SetLeft(myCircle, 10);
Canvas.SetRight(myCircle, 10);
Canvas.SetBottom(myCircle, 10);
Canvas.SetTop(myCircle, 10);
myCanvas.Children.Add(myCircle);
}
private void drawBlueCircle()
{
Ellipse myCircle = new Ellipse();
myCircle.Stroke = Brushes.Blue;
myCircle.Width = 30;
myCircle.Height = 30;
myCircle.StrokeThickness = 2;
Canvas.SetLeft(myCircle, 20);
Canvas.SetRight(myCircle, 20);
Canvas.SetBottom(myCircle, 20);
Canvas.SetTop(myCircle, 20);
myCanvas.Children.Add(myCircle);
}
If you give the added circle a name, you can find it when the checkbox is unchecked and then remove it pretty easily.
private string redCircleName = "redCircle";
private string blueCircleName = "blueCircle";
private void redCB_Checked(object sender, RoutedEventArgs e)
{
drawRedCircle();
}
private void redCB_Unchecked(object sender, RoutedEventArgs e)
{
RemoveCircleByName(redCircleName);
}
private void blueCB_Checked(object sender, RoutedEventArgs e)
{
drawBlueCircle();
}
private void blueCB_Unchecked(object sender, RoutedEventArgs e)
{
RemoveCircleByName(blueCircleName);
}
private void RemoveCircleByName(string name)
{
var circle = (UIElement)LogicalTreeHelper.FindLogicalNode(myCanvas, name);
myCanvas.Children.Remove(circle);
}
private void drawRedCircle()
{
Ellipse myCircle = new Ellipse();
myCircle.Stroke = Brushes.Red;
myCircle.Width = 30;
myCircle.Height = 30;
myCircle.StrokeThickness = 2;
//Give it a name here so we can find it later
myCircle.Name = redCircleName;
Canvas.SetLeft(myCircle, 10);
Canvas.SetRight(myCircle, 10);
Canvas.SetBottom(myCircle, 10);
Canvas.SetTop(myCircle, 10);
myCanvas.Children.Add(myCircle);
}
private void drawBlueCircle()
{
Ellipse myCircle = new Ellipse();
myCircle.Stroke = Brushes.Blue;
myCircle.Width = 30;
myCircle.Height = 30;
myCircle.StrokeThickness = 2;
//Give it a name here so we can find it later
myCircle.Name = blueCircleName;
Canvas.SetLeft(myCircle, 20);
Canvas.SetRight(myCircle, 20);
Canvas.SetBottom(myCircle, 20);
Canvas.SetTop(myCircle, 20);
myCanvas.Children.Add(myCircle);
}
这篇关于如何删除一个帆布的一个孩子后,我加入了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!