Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4345882
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:05:52+00:00 2026-05-21T12:05:52+00:00

I am new to WPF/XAML so please bear with the noob question. I have

  • 0

I am new to WPF/XAML so please bear with the noob question.

I have designed a control panel that will eventually function as a backend for my website, and have just finished laying out all the buttons in tabs using TabControl element. (this is designed using the Visual Studio ‘Window’ forms.

My question is, is it possible to create a function in the xaml.cs file that will dynamically handle a specific event for all my button elements ? for example…

I have 30+ buttons and dont want 30 different Click=”btnCustomers_click” + their respective functions in the c# code. What I desire is say one function that would allow me to click any button and then open a new window depending on which button was selected.

The below code is my current design however for 30+ buttons their will be alot of functions and it will be messy, hence my desire to have one function control which window is opened depending on which button is clicked.

        private void btnMerchants_click(object sender, RoutedEventArgs e)
    {
        var newWindow = new frmMerchants();
        newWindow.Show();
    }

Thanks in advance for any advice given!! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T12:05:53+00:00Added an answer on May 21, 2026 at 12:05 pm

    You could use a style for this:

    <Style TargetType="{x:Type Button}">
        <EventSetter Event="Click" Handler="btnMerchants_click"/>
    </Style>
    

    If you set this up in the resources somewhere without an x:Key it will apply to all buttons.


    e.g. if you have a Grid and you want a certain style to apply to all Buttons in it you would define it like this:

    <Grid>
        <Grid.Resources>
            <Style TargetType="{x:Type Button}">
                <EventSetter Event="Click" Handler="Button_Click"/>
            </Style>
        </Grid.Resources>
        <Grid.Children>
            <!-- Buttons and stuff -->
        </Grid.Children>
    </Grid>
    

    If you just want to apply it to some buttons set the x:Key and reference the style:

    <Grid>
        <Grid.Resources>
            <Style x:Key="AttachClickHandlerStyle" TargetType="{x:Type Button}">
                <EventSetter Event="Click" Handler="Button_Click"/>
            </Style>
        </Grid.Resources>
        <Grid.Children>
            <Button Content="Click me!" Style="{StaticResource AttachClickHandlerStyle}"/>
            <Button Content="Click me, too!" Style="{StaticResource AttachClickHandlerStyle}"/>
            <Button Content="Something different." Click="SomeOtherButton_Click"/>
        </Grid.Children>
    </Grid>
    

    In general you should refactor any attributes that occur more than once into a style to prevent duplicate code.

    Also, since you are a beginner the following articles might be of interest:

    Styling and Templating
    Resources Overview

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am (very) new to WPF and I have got a question regarding that.
I'm very new to WPF, so please bear with me. Basically I have defined
I am NEW to WPF. I have the following XAML code: </Window> ... <Canvas>
I am relatively new to .NET/C#/WPF/XAML. I have noticed sometimes controls seem to be
I am new to WPF and xaml and I have a problem with my
I have a custom WPF user control called a TimeoutPanel that I am trying
Couldn't find an answer to this one. I have a WPF ListView control that
I am new to WPF, please do not blame me ;) I have the
First of all, I am new to WPF and Xaml, so I just hope
In Visual Studio 2008, when you add a new XAML window to a WPF

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.