Search code examples
c#wpfstackpanelicommandrelaycommand

How to find out which button I pressed?


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. enter image description here

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?


Solution

  • 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.