如何找出我按下了哪个按钮? [英] How to find out which button I pressed?

查看:152
本文介绍了如何找出我按下了哪个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Facebook上查看通知下拉菜单. 我想实现类似的东西.单击"Slet"时,应该从列表中删除该通知.

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

基本上,我有一个垂直的堆栈面板,其中有x个水平的堆栈面板.每个按钮都有一个文本框和两个按钮. 我怎么知道我点击了哪个按钮?这些按钮都绑定到一个删除命令,但是我不确定它们是如何工作的:

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());
    }
}

然后创建此方法:

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?

推荐答案

您应该使用

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);

然后,在DeleteNotification方法中,您需要为密钥指定一个参数.

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

现在,在DeleteNotification中,您可以标识与按钮相关的通知.

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

这篇关于如何找出我按下了哪个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆