如何分配点击事件
我有一个在运行时动态生成的按钮数组。我的代码中有按钮单击的功能,但我找不到在代码中设置按钮的单击名称的方法。那么,
XAML 的等效代码是什么:
<Button x:Name="btn1" Click="btn1_Click">
或者,我应该为“????”放置什么?在下面的代码中:
Button btn = new Button();
btn.Name = "btn1";
btn.???? = "btn1_Click";
I have an array of button which is dynamically generated at run time. I have the function for button click in my code, but I can't find a way to set the button's click name in code. So,
what is the code equivalent for XAML:
<Button x:Name="btn1" Click="btn1_Click">
Or, what should I place for "????" in the following Code:
Button btn = new Button();
btn.Name = "btn1";
btn.???? = "btn1_Click";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以下应该可以解决问题:
The following should do the trick:
我认为 WPF 不支持您想要实现的目标,即使用方法名称或 btn1.Click =“btn1_Click”将方法分配给按钮。您将必须使用上面答案中建议的方法,即使用适当的方法注册按钮单击事件
btn1.Click += btn1_Click;
I don't think WPF supports what you are trying to achieve i.e. assigning method to a button using method's name or btn1.Click = "btn1_Click". You will have to use approach suggested in above answers i.e. register button click event with appropriate method
btn1.Click += btn1_Click;
你应该放在下面的行
You should place below line