I have a problem with adding parameters to EventHandler. I have a control when user select message and choose to who he want to send it.
I need to handler OnConfirmForwarClosed add in some way variable item.
How can I do it?
private void inboxContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
var item = (RadMenuItem)e.Source;
开发者_C百科 RadWindow.Confirm("Do you want forward this message to item.DataContext.HandlerName ?", OnConfirmForwarClosed);
}
private void OnConfirmForwarClosed(object sender, WindowClosedEventArgs e)
{
if (e.DialogResult == true)
{
//here I need item from caller
}
}
EDIT I need a result which I can get by this solution:
RadWindow.Confirm("Do you want forward this message to item.DataContext.HandlerName ?",(s,ea)=>
{
if(ea.DialogResult==true)
{
MessageBox.Show((item.DataContext as Handler).HandlerId.ToString());
}
});
If i understand your question correctly, you don't want to "lift" the local variable into lambda, such as...
void inboxContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e) {
var item = (RadMenuItem)e.Source;
RadWindow.Confirm(
"Do you want forward this message to item.DataContext.HandlerName ?",
(sender, e) => {
if (e.DialogResult == true) {
// You can use 'item' here directly.
}
}
);
}
...and instead you want to keep the OnConfirmForwarClosed
(presumably to subscribe to events not shown here). If that's correct, then you have couple of options:
1.
You could just arrange your methods differently:
void inboxContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e) {
var item = (RadMenuItem)e.Source;
RadWindow.Confirm(
"Do you want forward this message to item.DataContext.HandlerName ?",
(sender, e) => OnConfirmForwarClosedImp(sender, e, item)
);
}
void OnConfirmForwarClosedImp(object sender, WindowClosedEventArgs e, RadMenuItem item) {
if (e.DialogResult == true) {
if (item != null) {
// Use 'item'.
}
else {
// OnConfirmForwarClosed was called from somewhere else.
}
}
}
void OnConfirmForwarClosed(object sender, WindowClosedEventArgs e) {
OnConfirmForwarClosedImp(sender, e, null);
}
2.
Set the object field:
void inboxContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e) {
item = (RadMenuItem)e.Source;
try {
RadWindow.Confirm(
"Do you want forward this message to item.DataContext.HandlerName ?",
OnConfirmForwarClosed
);
}
finally {
item = null;
}
}
RadMenuItem item = (RadMenuItem)e.Source;
void OnConfirmForwarClosed(object sender, WindowClosedEventArgs e) {
if (e.DialogResult == true) {
if (item != null) {
// Use 'this.item'.
}
else {
// OnConfirmForwarClosed was called from somewhere else.
}
}
}
3.
Co-opt one of the existing OnConfirmForwarClosed
parameters:
void inboxContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e) {
var item = (RadMenuItem)e.Source;
RadWindow.Confirm(
"Do you want forward this message to item.DataContext.HandlerName ?",
(sender, e) => OnConfirmForwarClosed(item, e)
);
}
void OnConfirmForwarClosed(object sender, WindowClosedEventArgs e) {
if (e.DialogResult == true) {
var item = sender as RadMenuItem;
if (item != null) {
// Use 'item'.
}
else {
// OnConfirmForwarClosed was called from somewhere else.
}
}
}
Etc, etc...
精彩评论