Astrashar
Astrashar

Reputation: 27

How to generate event handler automatically in WPF, C#?

I'm currently learning to code WPF application after having used forms for a while. I have a simple question, but can't seem to find an anwser anywhere.

Is it possible to automatically generate the 'private void eventName(...' code when creating an event ?

For example, if I create a WPF Form with a simple button and code :

Button x:Name = "mButton" Content = "Hello" Click = "mClick" /

Is there a trick to have the private void event handler create itself ? Cause right now, I either write it manually or double click in the event handler properties tab. In widowsForm, I could just double click and it would create itself. This isn't a big issue for 1 or 2 but if I want to create a dozen buttons, it can become tedious.

I apologize if the question can seem obvious

Upvotes: 1

Views: 3609

Answers (2)

Bizhan
Bizhan

Reputation: 17085

Of-course, the more automated and lazier the better. So a few tips:

You can generate a new event handler with an automated name like this:

  • Assign the x:Name before creating or assigning the event handler
  • Pick the default <New Event Handler> from the list of options IDE gives you for your event handler. it will generate something like:

    MouseDoubleClick="mButton_MouseDoubleClick" or Click="mButton_Click"

If the name is already taken, it will be prefixed with _1

If the x:Name is not assigned, it will be prefixed with Button_ instead of x:Name


You can generate any already-written event handler like this:

  • Right click on handler's name in XAML code ("mClick") and choose Go To Definition (The default shortkey is F12)

F12 does the same thing as double-clicking on an event handler value in properties window in WinForms. In case of default event (like Button's Click, it does the same as double-clicking directly on the control)


If you don't want the control to contain any code for event handler like:

 <Button /> // handles the click event magically

Then you can add this to the container of all the buttons:

<Container.Resources>
    <Style TargetType="Button">
        <EventSetter Event="Click" Handler="mClick"/>
    </Style>
</Container.Resources>

(obviously, I supposed the name of the container is Container. In your case it might be Window or Menu etc.)

Now every button inside this container has its Click handled by the same handler, in which you can redirect your logic to the right method:

    Dictionary<string, Action> dic;
    private void mClick(object sender, RoutedEventArgs e)
    {
        dic[(sender as Button).Name]();
    }

These all still so tedious compared to MVVM pattern:

<ItemsControl ItemsSource="{Binding myButtons}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Content="{Binding ButtonText}" Command="{Binding ButtonAction}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Upvotes: 4

pete the pagan-gerbil
pete the pagan-gerbil

Reputation: 3166

If you want to do this rather than use a databinding/command pattern, you can use the XAML designer in Visual Studio. If you start typing Click=" you should be prompted with a list of possible event handlers or a new one - selecting one and pressing tab will create the event handler in the code behind for you (you might want to rename it, or make sure you name the button in XAML first).

Upvotes: 1

Related Questions