WPF,用后台代码创建了几个按钮,在运行的时候点击这些按钮,如何在点击按钮事件中知道点击的是哪个按钮呢

2024-10-31 16:36:07
推荐回答(1个)
回答1:

创建button的时候给button命名,然后在button的Click的事件中获取Name即可
var button = new Button { Name = "Button" };
button.Click+=new RoutedEventHandler(Button_Click);
private void Button_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
var buttonName = button.Name;
MessageBox.Show(buttonName);
}