radio.Checked += new RoutedEventHandler(VariantChecked);
assuming I have this code. What开发者_StackOverflow if I want to pass an argument to VariantChecked method. What syntax should I apply?
During creation, attach your data object to the DataContext or the Tag property of the RadioButton.
RadioButton radio=new RadioButton();
radio.DataContext=yourData;
Then in the event handler, get the data back:
void VariantChecked(object sender, RoutedEventArgs e){
RadioButton radio=(RadioButton)sender;
YourData yourData=(YourData)radio.DataContext;
}
In the above example I have assumed that you have a class or struct named YourData that you want to provide. You can replace this through any primitive like string or int or any other object type.
The above works also from xaml:
<RadioButton Tag="Static Data, could also be a binding" ...
Here I have taken the Tag property because it makes for such a construction more sense, but DataContext could also be taken. The event handler is the same except for casting from the Tag-property.
void VariantChecked(object sender, RoutedEventArgs e){
RadioButton radio=(RadioButton)sender;
string yourStringFromTag=(string)radio.Tag;
}
By the way, you can make the code more generic by not specify a concrete control-class but a base class:
void VariantChecked(object sender, RoutedEventArgs e){
FrameworkElement fe=(FrameworkElement)sender;
string yourStringFromTag=(string)fe.Tag;
}
Hope this helped...
精彩评论