-1

I'm creating Buttons programmatically with a method and am wanting to attach a Click event handler. However, that data currently comes from a string parameter which can't be used with += RoutedEventHandler.

public Button CreateButton(string Display, string Name, string ClickEventHandler)
{
    Button Btn = new Button
    {
        Content = Display,
        Name = "Btn_" + Name
    };
    Btn.Click += new RoutedEventHandler(ClickEventHandler);

    return Btn;
}

void Btn_save_Click(object sender, RoutedEventArgs e)
{
    throw new NotImplementedException();
}

// later
Button MyButton = CreateButton("Save", "save", "Btn_save_Click");

Error is RoutedEventHandler expects a Method and not a String. Is there a different approach to programmatically binding events that allows this sort of behaviour? Thanks

Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
Jalfie
  • 69
  • 1
  • 2
  • 8
  • 1
    a. Pass the method without quotes. b. In WPF don't use events, use command bindings. – Danny Varod May 15 '19 at 20:58
  • 1
    this is fundamentally wrong. should read the tutorial again https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/how-to-add-an-event-handler-using-code – Steve May 15 '19 at 20:58
  • @DannyVarod a bit too strict to say WPF doesnt use events. if the event is at view level then its perfectly fine. – Steve May 15 '19 at 20:59
  • 1
    @Steve I think what Danny meant was *you shouldn't use events*, instead you should use command bindings. He never said, "WPF doesn't use events"... – Trevor May 15 '19 at 21:07
  • @Steve, as Codexer said, I never said that. Just because you can do something, doesn't mean you should. – Danny Varod May 16 '19 at 18:27
  • Moreso, don't programmatically create buttons in WPF - you should bind a list of buttons to a list of commands in your view-model. Please do yourself a favor and read about mvvm in WPF, data-binding, command-binding etc. – Danny Varod May 16 '19 at 18:28

2 Answers2

6

I am not entirely sure what you are trying to accomplish with this.

Here is an example of how to create an event at run time.

public void CreateButton()
{
  Button Btn = new Button();

  Btn.Click += new EventHandler(btn_Clicked);


}

private void btn_Clicked(object sender, EventArgs e)
{
 // Your Logic here
}
Reese Jones
  • 181
  • 8
  • Just because this will work, doesn't mean this is a good practice. – Danny Varod May 16 '19 at 18:29
  • 1
    @DannyVarod So you just write that comment everywhere? I could see an argument about my answer, but this one is just the standard way to declare an event. – Arthur Rey May 16 '19 at 23:53
  • The answer is correct, I just wouldn't want to see people doing this. I recommend you show a better alternative. Some people actually learn from this site, so best to provide good practices and not just answer any question to get points. – Danny Varod May 20 '19 at 08:16
  • Late maybe... but indulge me @DannyVarod because I have an aeronautical science degree. I’ve done my best to learn the world I am in which involves a lot of hours on this site. If this isn’t up to current standards for someone getting into this industry then what do you recommend? – Reese Jones Aug 28 '19 at 02:46
  • 1
    @ReeseJones I recommend you read a bit about MVVM including data binding and command binding in WPF. It is a much cleaner way of doing things which has been around since the first version of WPF links https://blogs.msdn.microsoft.com/msgulfcommunity/2013/03/13/understanding-the-basics-of-mvvm-design-pattern/ and https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/commanding-overview – Danny Varod Aug 28 '19 at 11:20
  • @DannyVarod I have actually been learning and working with mvvm on a WPF application and have been doing a lot of control binding. For this post I was only giving an answer for the best way I knew and didn’t assume they were using WPF. I appreciate the feedback and link I will be using it going forward. – Reese Jones Aug 28 '19 at 11:55
  • Glad to hear, next time note the tags on the question. – Danny Varod Aug 28 '19 at 16:42
4

From what I understand you wish to pass the method that should be executed when Click event is triggered. You could do something along the lines of:

Button button = CreateButton("Save", "save", (s, e) => SomeOnClickEvent(s, e));
Button button2 = CreateButton("Create", "create", (s, e) => SomeOtherOnClickEvent(s, e));

public Button CreateButton(string display, string name, Action<object, EventArgs> click)
{
    Button b = new Button()
    {
        Content = display,
        Name = $"Btn_{name}"
    };

    b.Click += new EventHandler(click);

    return b;
}

void SomeOnClickEvent(object sender, EventArgs e)
{

}

void SomeOtherOnClickEvent(object sender, EventArgs e)
{

}
Arthur Rey
  • 2,990
  • 3
  • 19
  • 42