简体   繁体   中英

WPF / C# - Adding functionality to a button dynamically created inside a listbox

I have a button that adds this StackPanel to the listbox everytime it's clicked. In it is a button. I'm trying to figure out how to add code to this button that it's adding. Ideally I want the button to be a delete button, so it would delete that element (itself) in the list. I'm just trying to figure out how to add functionality to the button I'm dynamically creating. hope that makes sense

thanks for any help!

    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);
    }

You can programatically add a click handler to the button like this:

    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?

and then you need the click event handler

    void btn_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        // your code to execute when the button is clicked.
        stackPanel.Items.Remove(button);
    }

That is the simplest setup. Ideally you want more error handling etc.

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

Try This.

Add Stackpanel that have textblock and Button

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);
        }

Delete Button Click handler

  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);
            }
        }

To Find the Type to Object in the 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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM