2

我有一个按钮,每次单击时都会将此 StackPanel 添加到列表框中。里面是一个按钮。我试图弄清楚如何将代码添加到它正在添加的这个按钮。理想情况下,我希望该按钮成为删除按钮,因此它将删除列表中的该元素(本身)。我只是想弄清楚如何为我动态创建的按钮添加功能。希望这是有道理的

谢谢你的帮助!

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        StackPanel stackPanel = new StackPanel();
        stackPanel.Orientation = System.Windows.Controls.Orientation.Horizontal;

        CheckBox checkBox = new CheckBox();
        checkBox.IsChecked = true;

        TextBox textBox = new TextBox();
        textBox.Width = 100;
        textBox.Text = textBox1.Text;

        Button button = new Button();  //HOW DO I ADD CODE TO THIS BUTTON?

        stackPanel.Children.Add(checkBox);
        stackPanel.Children.Add(textBox);
        stackPanel.Children.Add(button);  //HOW DO I ADD CODE TO THIS BUTTON?

        listBox1.Items.Add(stackPanel);
    }
4

3 回答 3

2

您可以像这样以编程方式向按钮添加点击处理程序:

    Button button = new Button();  //HOW DO I ADD CODE TO THIS BUTTON?
    button.Click += btn_Click;

    stackPanel.Children.Add(checkBox);
    stackPanel.Children.Add(textBox);
    stackPanel.Children.Add(button);  //HOW DO I ADD CODE TO THIS BUTTON?

然后你需要点击事件处理程序

    void btn_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        // your code to execute when the button is clicked.
        stackPanel.Items.Remove(button);
    }
于 2013-09-25T07:12:58.867 回答
1

这是最简单的设置。理想情况下,您需要更多的错误处理等。

Button button = new Button();
button.Click += (s, args) => { listBox1.Items.Remove(stackPanel); };
于 2013-09-25T07:15:07.663 回答
1

尝试这个。

添加具有文本块和按钮的 Stackpanel

private void OnSaveClick(object sender, RoutedEventArgs e)
        {
            StackPanel stp = new StackPanel();
            stp.Orientation = Orientation.Horizontal;
            stp.Children.Add(new TextBlock()
            {
                 Text =  string.Format("Item {0}",  lstitems.Items.Count), 
                  HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch
            });

           Button btn = new Button();
           btn.Content = string.Format("Delete Item {0}",  lstitems.Items.Count);
           btn.Height = 25;
           btn.Width = 100;
           btn.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
           btn.Click += btnDeleteClick;


           stp.Children.Add(btn);

           lstitems.Items.Add(stp);
        }

删除按钮单击处理程序

  void btnDeleteClick(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            if (btn != null)
            {
               var st = FindParent<StackPanel> (btn); //stackpanel as we have added item as stackpanel.
               if (st != null)
                   lstitems.Items.Remove(st);
            }
        }

在可视化树中查找对象类型。

    public  T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
    {
        var parent = VisualTreeHelper.GetParent(dependencyObject);

        if (parent == null) return null;

        var parentT = parent as T;
        return parentT ?? FindParent<T>(parent);
    }
于 2013-09-25T11:18:08.127 回答