wpf 将自定义参数传递给事件处理程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14147779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing custom arguments to event handler
提问by Steven Batham
Possible Duplicate:
Passing arguments to event handler
可能的重复:将
参数传递给事件处理程序
I'm trying to handle an event raised from a MenuItem.Click. The catch is, I need some way to pass some custom arguments with it (specifically, two integers representing row and column).
我正在尝试处理从MenuItem.Click. 问题是,我需要某种方式来传递一些自定义参数(特别是,代表行和列的两个整数)。
private void Window_Loaded(object sender, RoutedEventArgs e)
{
...
MenuItem froNewChild = new MenuItem();
froNewChild.Header = "Insert new box";
froNewChild.Click += new RoutedEventHandler(froNewChild_Click);
// froNewChild.Click += new RoutedEventHandler(froNewChild_Click, column, row);
FlowRectangleOptions.Items.Add(froNewChild);
...
}
private void froNewChild_Click(object sender, RoutedEventArgs e) //+column & row
{
FlowDocument.allColumns[column-1].AddFlowRectangle(column, row);
FlowDocument.ReRenderAll(canvas1);
}
This answer Passing arguments to an event handlerwould seem to do what I want, but doesn't work as-is because senderis already defined in this scope. Sadly I don't know enough yet to work around that problem - advice greatly appreciated!
这个答案将参数传递给事件处理程序似乎可以做我想要的,但不能按原样工作,因为sender已经在这个范围内定义了。遗憾的是,我对解决该问题的了解还不够多 - 非常感谢您的建议!
回答by sa_ddam213
Assumming "column" and "row" are created in your loaded method, you could just use a Lambda event handler so you can pass the variables in.
假设在您加载的方法中创建了“列”和“行”,您可以只使用 Lambda 事件处理程序,以便您可以传入变量。
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
//Assumming "column" and "row" are in this block somewhere.
...
MenuItem froNewChild = new MenuItem();
froNewChild.Header = "Insert new box";
froNewChild.Click += (s, eArgs) =>
{
FlowDocument.allColumns[column - 1].AddFlowRectangle(column, row);
FlowDocument.ReRenderAll(canvas1);
};
FlowRectangleOptions.Items.Add(froNewChild);
...
}
回答by Felice Pollano
The argument passed to the evant handler depends on how the raising class is designed, and they are passed as a class derived from EventArgsas a second argument. Augmenting this is not possible unless you are the designer of the class raising the event. In this particular case we are talking about a framework class and so it is not possible at all.
传递给 evant 处理程序的参数取决于提升类的设计方式,它们EventArgs作为第二个参数作为派生自的类传递。除非您是引发事件的类的设计者,否则不可能增加此功能。在这种特殊情况下,我们谈论的是框架类,因此根本不可能。
As a workaround you can define some state member variable in your class and use them to pass informations to the handler.
作为一种解决方法,您可以在类中定义一些状态成员变量,并使用它们将信息传递给处理程序。
回答by AgentFransis
sa_ddam213 already answered you but just to clarify: the parameter names in lambda expressions carry no semantic meaning and can be whatever you want. The names sender and eventArgs were only used for clarity in the question you linked. What matters in this case is that there are two params and the compiler infers the rest by himself.
sa_ddam213 已经回答了你,但只是为了澄清:lambda 表达式中的参数名称没有语义意义,可以是任何你想要的。名称 sender 和 eventArgs 仅用于在您链接的问题中清楚起见。在这种情况下重要的是有两个参数,编译器自己推断其余的。
The values of column and row will be captured at the point you define the lambda and will be available when the event handler executes even if MainWindow_Loaded has long ago returned and the original variables destroyed.
column 和 row 的值将在您定义 lambda 时捕获,并且在事件处理程序执行时可用,即使 MainWindow_Loaded 很久以前返回并且原始变量已销毁。
For more about lambda expressions in C#: http://msdn.microsoft.com/en-us/library/bb397687.aspx
有关 C# 中 lambda 表达式的更多信息:http: //msdn.microsoft.com/en-us/library/bb397687.aspx

