How to find out which button I pressed?

耗尽温柔 提交于 2019-12-24 23:48:51

问题


Picture the notification-dropdown menu from Facebook. I want to implement something similar. When clicking 'Slet', it's supposed to delete that notification from the list.

private void AddNotificationsToPanel(List<Notification> notifications, StackPanel panel)
{
    panel.Children.Clear();


    foreach (var notification in notifications)
    {
        //We want every message to have text, a delete button and a postpone button
        //So we need a nested stackpanel:
        var horizontalStackPanel = new StackPanel();
        horizontalStackPanel.Orientation = Orientation.Horizontal;
        panel.Children.Add(horizontalStackPanel);

        //Display the message:
        var text = new TextBlock();
        text.Text = notification.Message;
        text.Foreground = Brushes.Black;
        text.Background = Brushes.White;
        text.FontSize = 24;
        horizontalStackPanel.Children.Add(text);

        //Add a delete button:
        var del = new Button();
        del.Content = "Slet";
        del.FontSize = 24;
        del.Command = DeleteNotificationCommand;
        horizontalStackPanel.Children.Add(del);

        //Add a postpone button:
        var postpone = new Button();
        postpone.Content = "Udskyd";
        postpone.FontSize = 24;
        postpone.IsEnabled = false;
        horizontalStackPanel.Children.Add(postpone);
    }
    panel.Children.Add(new Button { Content = "Luk", FontSize = 24, Command = ClosePopupCommand });
}

Basically, I have a vertical stackpanel with x amount of horizontal stackpanels. Each of those have a textbox and two buttons. How do I know which button I clicked? The buttons are all bound to a delete command, but I'm kind of unsure how these work:

public ICommand DeleteNotificationCommand
{
    get{
        return new RelayCommand(o => DeleteNotification());
    }
}

Which then create this method:

private void DeleteNotification()
{
    Notifications.Remove(NotificationForDeletion);
    AddNotificationsToPanel(Notifications, Panel);
}

Problem is we don't know which Notification to delete, because I don't know how to see which button was clicked. Any ideas?


回答1:


You should use CommandParameter property of the button by assigning unique identifier of each notification to it. I'm assuming your notification has an unique integer id:

 //Add a delete button:
 var del = new Button();
 del.Content = "Slet";
 del.FontSize = 24;
 del.Command = DeleteNotificationCommand;
 del.CommandParameter = notification.Id; // <-- unique id
 horizontalStackPanel.Children.Add(del);

Then in the DeleteNotification method, you need to specify a parameter for the key.

public ICommand DeleteNotificationCommand
{
    get{
        return new RelayCommand(DeleteNotification);
    }
}     
private void DeleteNotification(object parameter)
{
    int notificationId = (int)parameter;
    var NotificationForDeletion = ...;  // <--- Get notification by id
    Notifications.Remove(NotificationForDeletion);
    AddNotificationsToPanel(Notifications, Panel);
}

Now, in the DeleteNotification you can identify the Notification that is related to the button.



来源:https://stackoverflow.com/questions/30015050/how-to-find-out-which-button-i-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!