I want to dynamically add Commands to buttons. Each button should execute a function using the iterator variable as a parameter. The problem is, while the button text displays correctly, "0, 1, 2" on the buttons, the commands are all executed with the final value of the iterator, "SomeCommand(2)". How do I get the buttons to execute SomeCommand(0), SomeCommand(1) and SomeCommand(2) respectively?
public void DynamicButtons()
{
for(int i = 0; i < 3; i++)
{
Button newButton = new Button { Command = new Command(() => { SomeFunction(i); }), Text = i.ToString() };
}
}