Handle a Button Click with Shared button click handler : Command « Windows Presentation Foundation « C# / C Sharp






Handle a Button Click with Shared button click handler

Handle a Button Click with Shared button click handler
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="200" Width="200">
    <StackPanel>
        <Button Click="SharedButtonClickHandler" Height="23" Margin="10" 
                Name="button1" Width="75">Button One</Button>
        <Button Click="SharedButtonClickHandler" Height="23" Margin="10" 
                Name="button2" Width="75">Button Two</Button>
        <Button Click="SharedButtonClickHandler" Height="23" Margin="10" 
                Name="button3" Width="75">Button Three</Button>
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void SharedButtonClickHandler(object sender, RoutedEventArgs e)
        {
            Button source = e.OriginalSource as Button;

            if (source != null)
            {
                MessageBox.Show("You pressed " + source.Name, Title);
            }
        }
    }
}

   
    
  








Related examples in the same category

1.Built-In Command BindingsBuilt-In Command Bindings
2.Help commandHelp command
3.Button CommandTarget BindingButton CommandTarget Binding
4.Creating CommandBinding and attaching an Executed and CanExecute handlerCreating CommandBinding and attaching an Executed and CanExecute handler
5.Creating a KeyBinding between the Open command and Ctrl-RCreating a KeyBinding between the Open command and Ctrl-R
6.Command Handler Command Binding in XamlCommand Handler Command Binding in Xaml
7.Command Handler Key BindingCommand Handler Key Binding
8.Bind the Button to a CommandBind the Button to a Command
9.Command EnablingCommand Enabling
10.Using CommandBinding and ExecutedRoutedEventHandler to bind Application event to an event handler methodUsing CommandBinding and ExecutedRoutedEventHandler to bind Application event to an event handler method
11.Create CommandBindings in Xaml and bind to ButtonCreate CommandBindings in Xaml and bind to Button
12.Bind TextBox save command to CommandBindingBind TextBox save command to CommandBinding
13.Use TextBox.CommandBindingst to bind commandUse TextBox.CommandBindingst to bind command
14.Parse command line arguments and make them available to an application.