如何在运行时将信息从控件传递到事件

时间:2011-02-27 05:18:30

标签: c#

我正在使用FlowLayoutPanel内的代码创建按钮,我无法为每个创建的按钮创建一个click事件。我需要一些帮助,因为我坚持这个。

2 个答案:

答案 0 :(得分:0)

您可以在创建每个按钮Click event时订阅它们。

例如:

Button newButton = new Button();
// Setup button...
newButton.Click += this.ClickHandler;

然后,在其他地方:

private void ClickHandler(object sender, RoutedEventArgs e)
{
      // Do something here
}

另一个可能更优雅的选择是将Button的Command绑定到ICommand。您还可以将其设置为CommandParameter,以便传递其他特定于按钮的信息。

答案 1 :(得分:0)

以下是解决方案Manage controls at Runtime

相关问题