WPF / C# - 为在列表框内动态创建的按钮添加功能

时间:2013-09-25 07:09:52

标签: c# wpf listbox

我有一个按钮,每次单击时都会将此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);
    }

3 个答案:

答案 0 :(得分: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);
    }

答案 1 :(得分:1)

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

Button button = new Button();
button.Click += (s, args) => { listBox1.Items.Remove(stackPanel); };

答案 2 :(得分: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);
            }
        }

在Visual Tree中查找要对象的类型。

    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);
    }
相关问题