如何找出我按下了哪个按钮?
How to find out which button I pressed?
想象一下来自 Facebook 的通知下拉菜单。
我想实现类似的东西。单击 'Slet' 时,应该从列表中删除该通知。
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 个水平堆栈面板的垂直堆栈面板。每个都有一个文本框和两个按钮。
我怎么知道我点击了哪个按钮?这些按钮都绑定到删除命令,但我不确定它们是如何工作的:
public ICommand DeleteNotificationCommand
{
get{
return new RelayCommand(o => DeleteNotification());
}
}
然后创建这个方法:
private void DeleteNotification()
{
Notifications.Remove(NotificationForDeletion);
AddNotificationsToPanel(Notifications, Panel);
}
问题是我们不知道要删除哪个通知,因为我不知道如何查看单击了哪个按钮。有什么想法吗?
您应该通过为每个通知分配唯一标识符来使用按钮的 CommandParameter 属性。我假设您的通知有一个唯一的整数 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方法中,需要为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 中,您可以识别与按钮相关的通知。
想象一下来自 Facebook 的通知下拉菜单。
我想实现类似的东西。单击 'Slet' 时,应该从列表中删除该通知。
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 个水平堆栈面板的垂直堆栈面板。每个都有一个文本框和两个按钮。 我怎么知道我点击了哪个按钮?这些按钮都绑定到删除命令,但我不确定它们是如何工作的:
public ICommand DeleteNotificationCommand
{
get{
return new RelayCommand(o => DeleteNotification());
}
}
然后创建这个方法:
private void DeleteNotification()
{
Notifications.Remove(NotificationForDeletion);
AddNotificationsToPanel(Notifications, Panel);
}
问题是我们不知道要删除哪个通知,因为我不知道如何查看单击了哪个按钮。有什么想法吗?
您应该通过为每个通知分配唯一标识符来使用按钮的 CommandParameter 属性。我假设您的通知有一个唯一的整数 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方法中,需要为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 中,您可以识别与按钮相关的通知。