【问题标题】:How do I delete dynamically created button control from canvas using WPF如何使用 WPF 从画布中删除动态创建的按钮控件
【发布时间】:2010-12-16 06:46:32
【问题描述】:

我使用 WPF 在画布控件上动态创建了近 40 到 60 个按钮控件。现在我想从画布中删除选定的按钮控件。我该怎么做。

【问题讨论】:

    标签: .net wpf button canvas


    【解决方案1】:

    这是您可以动态删除控制的方式

    Label lbl = (Label)LogicalTreeHelper.FindLogicalNode(mystackpanel, "labelname");
    mystackpanel.Children.Remove(lbl);
    

    【讨论】:

      【解决方案2】:
      Button b=new Button();
      
      b=(Button)sender;
      
      grid.Children.Remove(b);
      

      【讨论】:

        【解决方案3】:

        最简单的方法是隐藏它,然后您不必担心处置它,因为这将由控件的常规生命周期处理。

        【讨论】:

          【解决方案4】:
          canvas.Children.Remove(buttonInstance);
          

          【讨论】:

          • 这样更可取,因为 GC 会删除该实例(假设它没有其他引用)。
          猜你喜欢
          • 2013-09-05
          • 1970-01-01
          • 2011-11-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多