This is probably a REALLY dumb question, but on the upside I don't think anyone's asked it here before(?)
The default number of parameters in all the button_clicked subscriber methods is two:
(object sender, RoutedEventArgs e)
I recently read that the arguments for the event are defined within the delegate. Now is this just one delegate for each button_clicked event? Or does each button have it's own separate delegate? I'm not explaining myself very well here...
Say I want to pass in three arguments into a single subscriber method instead of the above TWO, I've just had some simple code auto-generated for me after double clicking my button in designer view. Where is the class that that method subscribes to? Is there a way I can edit it's delegate and add in another parameter?
The reason I'm asking this WAS that I thought there could be a way to pass a variable to another class - into it's subscriber method. Unfortunately that subscriber method only takes two arguments. So I'm now looking for ways to easily pass in my variable, and this was a 'back-of-the-mind' sort of solution.
You guys are waaaayy overthinking this :). Simply use a lamda and you can redefine any delegate however you want:
theButton.Click += (x, y) => myHandler(someRandomParam1, someRandomParam2, someRandomParam3, someRandomParam4, ... someRandomParam73);
It's that simple.
You can set the Tag
parameter of the button and then retrieve it in the handler.
If the button is defined in XAML:
If the button is defined in code:
var button = new Button() {Tag = "Hi"};
Then in the event handler:
public void Click(object sender, RoutedEventArgs e)
{
// this will give you the string "Hi"
string hi = (string)((Button)sender).Tag;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.