0

我有一个 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);
    }
4

3 回答 3

3

如果您为添加的圆圈命名,您可以在未选中复选框时找到它,然后很容易将其删除。

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);
}
于 2012-11-16T10:12:08.127 回答
1

我认为unchecked事件调用稍后,这意味着由事件创建的圆圈被checked事件清除unchecked

一种解决方案是将创建和清除圆圈的逻辑移至单个方法,并且仅注册checked事件。

private void drawCircle()
{
       myCanvas.Children.Clear();

       if(redCB.Checked) drawRedCircle();
       if(blueCB.Checked) drawBlueCircle();     
}

private void redCB_Checked(object sender, RoutedEventArgs e)
{
   drawCircle();
}

private void blueCB_Checked(object sender, RoutedEventArgs e)
{
   drawCircle();
}
于 2012-11-16T09:34:48.477 回答
0

而不是添加/删除为什么不使用椭圆的可见性(IsVisible)?将它们绑定到通知布尔属性,并且您还将复选框绑定到该属性。

// ellipse1Visible is a notifying boolean 
Binding ellipseBinding = new Binding("ellipse1Visible");
ellipseBinding .Source = ??? ;   // set your binding source here (? this ?)
myEllipse.SetBinding(Ellipse.IsVisibleProperty, ellipseBinding );
myRelatedCheckBox.SetBinding(CheckBox.IsCheckedProperty, ellipseBinding)

如果要使用任意数量的椭圆,则必须使用“通知布尔值”的 Array/List/ObservableColection,语法为:

 // i is a valid index in the ellipseVisible collection of notifying boolean.
 Binding ellipseBinding = new Binding("ellipseVisible[" & i & "]");
 // the rest of the code is the same

您可以在 StackPanel/listBox/... 附近添加复选框,同时在画布中添加相关椭圆。

无需添加/删除椭圆,也无需使用此解决方案处理选中/取消选中。

Rq :如果您打算不使用每个椭圆的可见性,您甚至可以以更简单的方式做到这一点:没有布尔值,而是直接将可见性绑定到 CheckBox 的 IsChecked 属性:

// ... we just created myEllipse and myCheckBox
// ...  and inserted them into canvas/stackPanel 
Binding ellipseBinding = new Binding("IsChecked");
ellipseBinding.Source  = myCheckBox; 
myEllipse.SetBinding(Ellipse.IsVisibleProperty, ellipseBinding);
// and that's all
于 2012-11-16T16:53:25.933 回答