Button click action : Button Action « Windows Presentation Foundation « C# / CSharp Tutorial






<Window x:Class="EightBall.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Eight Ball Answer" Height="328" Width="412" >
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="*" />
      <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.Background>
      <LinearGradientBrush>
        <LinearGradientBrush.GradientStops>
          <GradientStop Offset="0.00"  Color="Red" />
          <GradientStop Offset="0.50" Color="Indigo" />
          <GradientStop Offset="1.00" Color="Violet" />
        </LinearGradientBrush.GradientStops>
      </LinearGradientBrush>
    </Grid.Background>
    <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,10,13,10" Name="txtQuestion" 
             TextWrapping="Wrap" FontFamily="Verdana" FontSize="24"
             Grid.Row="0" >
      [Place question here.]
    </TextBox>
    <Button VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,0,0,20" Width="127" Height="23" Name="cmdAnswer"
            Click="cmdAnswer_Click" 
            Grid.Row="1">      
      Ask the Eight Ball
      </Button>
    <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,10,13,10" Name="txtAnswer" 
             TextWrapping="Wrap" IsReadOnly="True" FontFamily="Verdana" FontSize="24" Foreground="Green"
             Grid.Row="2">
      [Answer will appear here.]
    </TextBox>    
  </Grid>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Input;

namespace EightBall
{
    public partial class Window1 : Window
    {

        public Window1()
        {
            InitializeComponent();
        }

        private void cmdAnswer_Click(object sender, RoutedEventArgs e)
        {           
            this.Cursor = Cursors.Wait;
            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));

            txtAnswer.Text = "asdf";
            this.Cursor = null;
        }

    }
}
WPF Button Click Action








24.4.Button Action
24.4.1.Button Click event handlerButton Click event handler
24.4.2.Use Button IsMouseOver Event tro Trigger an ActionUse Button IsMouseOver Event tro Trigger an Action
24.4.3.implementation of button's Click event handler in Xamlimplementation of button's Click event handler in Xaml
24.4.4.Button mouse down eventButton mouse down event
24.4.5.Button mouse down preview eventButton mouse down preview event
24.4.6.Button PreviewMouseDown action and MouseDown actionButton PreviewMouseDown action and MouseDown action
24.4.7.Button click actionButton click action
24.4.8.Dynamically add Button to a Grid and add Action listenerDynamically add Button to a Grid and add Action listener
24.4.9.Do event based on button nameDo event based on button name
24.4.10.Button PreviewMouseLeftButtonDown action and MouseLeftButtonDown actionButton PreviewMouseLeftButtonDown action and MouseLeftButtonDown action
24.4.11.Check if mouse left or right button clicked
24.4.12.Get mouse clicked button with MouseButtonEventArgs.ChangedButton
24.4.13.Adding click handler for Button
24.4.14.Add application command to a Button, and listen to application paste command
24.4.15.Button mouse enter and leave event
24.4.16.Button click event listener
24.4.17.Anonymous delegate as Button Action