我有一个 WPF 项目,我必须在画布上创建一些椭圆。我创建了两个复选框,当我选中第一个复选框时,红色椭圆将显示在画布上。如果我取消选中第一个复选框,椭圆将消失......第二个复选框将通过创建一个蓝色椭圆来具有相同的功能。
所以这是我的情况,当两个复选框被选中时,会出现一个蓝色和一个红色的椭圆。要清除画布上的椭圆,我使用myCanvas.children.clear()
. 但是当我取消选中其中一个复选框时,两个椭圆都将被删除。
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);
}