繁体   English   中英

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

[英]UWP C# How To Handle Event for Dynamically Created Button(s) and Control

关于帖子UWP C#动态添加按钮并在StackPanel上组织我还有其他问题

  1. 如何控制这些动态创建的按钮的事件? 例如。 按钮1打开LED 1,按钮2打开LED 2,依此类推。
  2. 如何有选择地删除按钮并重新组织其余的按钮,中间没有空格。

谢谢。

更新:我有一个例程,可以从客户端添加带有client IP等详细信息的客户端,并将其添加并显示在scrollviewer 我如何使用连结clientnameclient 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();
        });
    }

第一个问题:
为了解决这个问题,您应该引入一个字典,其中的按钮是键,而您的值是客户。 因此,您可以在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,您将在其中获取按钮的列和行,应将其删除。 之后,您可以操纵所有其他按钮的column和row属性。 为避免新添加的按钮与其他按钮不对齐,您需要更改添加过程,以根据字典中元素的数量确定新按钮的位置。 下面是完整代码的示例:

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

注意:重新安排过程未对性能进行优化。

您还可以使用Button的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);
    }

暂无
暂无

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

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