代码之家  ›  专栏  ›  技术社区  ›  mylim

UWP C#如何处理动态创建的按钮和控件的事件

  •  0
  • mylim  · 技术社区  · 6 年前

    关于职位 UWP C# Add Button Dynamically and Organizing On StackPanel 我还有其他问题

    1. 如何控制这些动态创建的按钮事件?例如,按钮1打开LED 1,按钮2打开LED 2等。

    非常感谢。

    更新: 我有一个程序来添加客户端的细节,如 client IP 等,并在 scrollviewer . 如何链接 clientname client ip

    private async void AddClientList()
        {
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                ClientListUserControl clientListControl = new ClientListUserControl(this, new ClientList(clientName, receiveIP, DateTime.Now, receivePort, receiveService, receiveDEV, receiveSTS, receiveACT));
                ClientList_Panel.Children.Add(clientListControl);
                clientListControl.updateDisplay();
            });
        }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Michael Xu    6 年前

    你也可以使用 Tag 传递参数的按钮的属性。此属性继承自FrameworkElement,通常用于获取或设置可用于存储有关此对象的自定义信息的任意对象值。

    请参考以下代码。

        private void ButtonCreateNewButton_Click(object sender, RoutedEventArgs e)
        {
            Button b = new Button();
            b.Height = 30;
            b.Width = 100;
            b.VerticalAlignment = VerticalAlignment.Top;
            b.HorizontalAlignment = HorizontalAlignment.Left;
            b.Margin = new Thickness(6, 6, 6, 6);
            b.Content = "Button " + buttonCounter;
            b.Tag = "LED-" + buttonCounter;
            b.Click += Button_Click;
    
            ....
    
            buttonCounter++;
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            var led = btn.Tag;
            //use led_name as a parameter here, according with this variable to turn on the LED
            TurnOnOffLed(led);
        }
    
        2
  •  1
  •   TheTanic Valentine    6 年前

    关于你的第一个问题:
    要处理这个问题,您应该引入一个字典,其中按钮是键,您的值是客户机。因此,您可以在ClickHandler中获取链接的客户端。

    public Dictionary<Button, object> clientDict = new Dictionary<Button, object>();
    

    注意

    在AddButton例程中添加按钮。再说一次:我不知道你从哪里得到你的客户,所以我增加了价值 null . 更改此项以满足您的要求。然后添加另一个ClickHandler并获取链接的客户端:

     b.Click += HandleButtonClick;
     clientDict.Add(b, null);
    
    
    
     private void HandleButtonClick(object sender, RoutedEventArgs e)
            {
                //Execute whatever you want from your client:
                var client = clientDict[sender as Button];
            }
    

    关于你的第二个问题:
    您需要添加一个RemoveMethod,从中可以得到按钮的列和行,应该将其删除。之后,您可以操作列和行属性的所有其他按钮。为了避免新添加的按钮与其他按钮不对齐,您需要更改添加过程,使新按钮的位置取决于词典中的元素数。下面是完整代码的示例:

    public int buttonCounter = 1;
    public Dictionary<Button, object> clientDict = new Dictionary<Button, object>();
    
    private void RemoveBtn(Button button)
    {
        var row = Grid.GetRow(button);
        var column = Grid.GetColumn(button);
    
        //Rearange
        foreach (var btn in clientDict.Keys)
        {
            var r = Grid.GetRow(btn);
            var c = Grid.GetColumn(btn);
    
            if (c > column || (c == column && r > row))
            {
                if (r != 0)
                {
                    //Set the row new
                    Grid.SetRow(btn, r - 1);
                }
                else
                {
                    //Need to set it to a new column
                    Grid.SetRow(btn, 3);
                    Grid.SetColumn(btn, c - 1);
                }
            }
        }
        myGrid.Children.Remove(button);
        clientDict.Remove(button);
    }
    
    private void Button_Click(object sender, RoutedEventArgs e)
    {
    
        //Create the button
        Button b = new Button();
        b.Height = 30;
        b.Width = 100;
        b.VerticalAlignment = VerticalAlignment.Top;
        b.HorizontalAlignment = HorizontalAlignment.Left;
        b.Margin = new Thickness(20, 20, 0, 0);
        b.Content = "Button " + buttonCounter;
        b.Click += HandleButtonClick;
        clientDict.Add(b, null);
    
        //Calculate the place of the button
        int column = (int)(clientDict.Count / 4);
        int row = clientDict.Count % 4;
    
        //Check if you need to add a columns
        if (row == 0 && myGrid.ColumnDefinitions.Count <= column)
        {
            ColumnDefinition col = new ColumnDefinition();
            col.Width = new GridLength(column, GridUnitType.Auto);
            myGrid.ColumnDefinitions.Add(col);
        }
    
        //Add the button
        myGrid.Children.Add(b);
        Grid.SetColumn(b, column);
        Grid.SetRow(b, row);
        buttonCounter++;
    }
    
    private void HandleButtonClick(object sender, RoutedEventArgs e)
    {
        //Execute whatever you want from you handler:
        var client = clientDict[sender as Button];
    }
    

    注: