通过按钮单击方法作为代码后面的参数? [英] Pass button click method as a parameter in code behind?

查看:68
本文介绍了通过按钮单击方法作为代码后面的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据模板,其中包含一个绑定到某些文本的按钮,以及一个硬编码的点击处理程序。



< DataTemplate DataType ={x :输入local:ContentClass}/>

< Button Content ={Binding MyContent}Click =Button_Click/>

< / DataTemplate>

 public class ContentClass 
{
public string MyContent {get;组; }

public ContentClass (字符串内容)
{
MyContent = content;
}
}

我可以使用以下代码显示带有hello文本的按钮:

 ContentClass foo = new ContentClass(你好); 

这完美无缺。



我需要做的是:

 ContentClass foo = new ContentClass(hello,Method1); 

然后,当您单击按钮时, Method1 被执行(将用后面的代码编写)。



在我所有的 ICommand 示例中在搜索网络时遇到,没有人能说明如何做到这一点。他们告诉所有关于 ICommand 如何工作,但没有人真正展示如何连接一个,更不用说在后面的代码中进行参数化。



任何帮助将不胜感激。



我尝试过:



我试过实现 ICommand 接口, RelayCommand 等等。代码示例编译,但是按下按钮时没有任何反应。当我单步执行时,没有任何断点点击(???)。

解决方案

如果我理解正确,您希望将事件处理程序作为参数传递到您的类中。



所以,这样的事情应该有效(但我没有测试过它):

  public   class  ContentClass 
{
public string MyContent { get ; set ; }

public ContentClass( string content,对象发​​件人,EventHandler eh)
{
MyContent = content;
sender.Click + = eh;
}
}





但我认为你是进入错误的方向,因为WPF框架与WinForm技术不同,并使用 RoutedEvents



请阅读:

活动(WPF) [ ^ ]

路由事件概述 [ ^ ]

如何:使用代码添加事件处理程序 [ ^ ]

如何:创建自定义路由事件 [ ^ ]




如何:提高和消费事件 [ ^ ]

处理和举办活动 [ ^ ]

I have a data template with a button that binds to some text, and a click handler that is hard coded.

<DataTemplate DataType="{x:Type local:ContentClass}"/>
<Button Content = "{Binding MyContent}" Click = "Button_Click"/>
</DataTemplate>

public class ContentClass
{
   public string MyContent { get; set; }

   public ContentClass(string content)
   {
      MyContent = content;
   }
}

I can display a button with text "hello" with the following code:

ContentClass foo = new ContentClass("hello");

This works perfectly.

What I NEED is to be able to do is this:

ContentClass foo = new ContentClass("hello", Method1);

Then, when you click the button, Method1 is executed (which will be written in code behind).

Of all the ICommand examples I have encountered while scouring the web, none can show how to do this. They tell all about how ICommand works, but no one actually shows how to wire one up, let alone parameterize it in code behind.

Any help would be appreciated.

What I have tried:

I have tried implementing the ICommand interface, RelayCommand, etc. The code examples compile, but nothing happens when the button is pressed. No breakpoints hit anywhere when I step through (???).

解决方案

If i understand you correctly, you want to pass event handler as a parameter into your class.

So, something like this should work (but i haven't tested it):

public class ContentClass
{
   public string MyContent { get; set; }
 
   public ContentClass(string content, Object sender, EventHandler eh)
   {
      MyContent = content;
      sender.Click += eh;
   }
}



But i think you're going into wrong direction, because WPF framework differs from WinForm technology and uses RoutedEvents.

Please, read this:
Events (WPF)[^]
Routed Events Overview[^]
How to: Add an Event Handler Using Code[^]
How to: Create a Custom Routed Event[^]



How to: Raise and Consume Events[^]
Handling and Raising Events[^]


这篇关于通过按钮单击方法作为代码后面的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆