简体   繁体   English

如何在c#wpf中为动态创建的按钮添加单击处理程序(未创建对象)

[英]how to add a click handler to dynamic created button in c# wpf (an object is not created)

by using the code below(code1) i created a dynamic stack panel to add buttons by an option file generated from before to my program but i wonder how to add click listener to each button generated since i cant create an object from each button. 通过使用下面的代码(代码1),我创建了一个动态堆栈面板,以通过从之前生成的选项文件向我的程序添加按钮,但是我不知道如何将点击侦听器添加到生成的每个按钮,因为我无法从每个按钮创建对象。

i tried adding click listener by using the mentioned code (code2) but it was not a success and also i tend to go throw the controls and find each button but i got null refrence exception (code 3) 我尝试通过使用提到的代码(code2)添加点击侦听器,但未成功,而且我倾向于抛出控件并找到每个按钮,但我得到了null异常(代码3)

//code1
StackPanel option_row = new StackPanel();
option_row.Name = "option_row" + i.ToString();
option_row.Children.Add(
               new Button
               {
                   Name = "write_btn" + i.ToString(),
                   VerticalAlignment = VerticalAlignment.Top,
                   Margin = new Thickness(5),
                   Content = "Write",
                   Height = 55,
                   Width = 80
               }
               );
//code2
                option_row.Children.Add(
               new Button
               {
                   Name = "write_btn" + i.ToString(),
                   VerticalAlignment = VerticalAlignment.Top,
                   Margin = new Thickness(5),
                   Content = "Write",
                   Height = 55,
                   Width = 80
               }.Click += new RoutedEventHandler(home_read_click)
               );

private void home_read_click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
        }
//code3
private void btn_sorter()
        {
            for (int i = 0; i < options_num; i++)
            {
                StackPanel x =(StackPanel) FindName("option_row" + i.ToString());
                foreach (Button item in x.Children)
                {
                    if (item.Name.Contains("read"))
                    {
                        home_read_buttons[i] = item.Name;
                    }
                    else if (item.Name.Contains("write"))
                    {
                        home_write_buttons[i] = item.Name;
                    }

                }
       }

        }

erors in code 2: The best overloaded method match for 代码2中的错误:最佳重载方法匹配

System.Windows.Controls.UIElementCollection.Add(System.Windows.UIElement)' has some invalid arguments
Argument 1: cannot convert from 'void' to 'System.Windows.UIElement'

you should create Button object at first, then assign it to 'option_row.Children' like this: 您应该首先创建Button对象,然后将其分配给'option_row.Children',如下所示:

var btn = new Button
           {
               Name = "write_btn" + i.ToString(),
               VerticalAlignment = VerticalAlignment.Top,
               Margin = new Thickness(5),
               Content = "Write",
               Height = 55,
               Width = 80
           };
 btn.Click += new RoutedEventHandler(home_read_click);
 option_row.Children.Add(btn);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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