Visual Studio 2019 (WPF C# Source Code & XAML) Using the Grid Layout and Button Controls


Visual Studio 2019 (WPF C# Source Code & XAML) Using the Grid Layout and Button Controls.

🎓 Master Visual Basic .NET and Access Database By Building the Point Of Sale System (POS).
ðŸ“ē Enroll Now: https://bit.ly/2WcbRhX

In this video tutorial you will learn: 
➜ How to use the Grid Layout and Button Controls.
➜ How to Add an Event Handler Using Code.
➜ How to Add a Button Click Event Handler in WPF (Attach single click event handler with multiple buttons).

🚀 For Visual Basic .NET, see: https://youtu.be/Lo-cX1AjBps

[Screenshot]


[YouTube]

[XAML & C# Code Behind]
Short link: http://bit.ly/2DjpTEL

MainWindow.xaml

<Window x:Class="ButtonAndGridControlWPFCS.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="WPF (C#) : Button and Grid Example : Code A Minute : iBasskung Tutorial"
        Height="570" Width="750"
        Background="DarkTurquoise"
        WindowStartupLocation="CenterScreen">
 
    <!-- 2 Columns and 3 Rows -->
 
    <Grid ShowGridLines="True" Margin="10">
     
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
     
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <Button Name="firstRowButton1"
                Grid.Column="0"
                Grid.ColumnSpan="2"
                Grid.Row="0"
                Content="(:  My Button 1  :)"
                HorizontalAlignment="Stretch"
                HorizontalContentAlignment="Center"
                VerticalAlignment="Center"
                VerticalContentAlignment="Center"
                FontSize="52"
                FontWeight="Bold"
                FontStyle="Oblique"
                Width="705"
                Height="155"
                Background="Crimson"
                Foreground="White"
                BorderBrush="White"
                BorderThickness="5"
                Margin="0"
                Padding="0"
                Click="FirstRowButton1_Click"/>

        <Button Name="secondRowButton1"
                Grid.Column="0"
                Grid.ColumnSpan="1"
                Grid.Row="1"
                Content="Button 2"
                HorizontalAlignment="Left"
                HorizontalContentAlignment="Left"
                VerticalAlignment="Top"
                VerticalContentAlignment="Top"
                FontSize="52"
                FontWeight="Bold"
                FontStyle="Normal"
                Width="330"
                Height="150"
                Background="MediumVioletRed"
                Foreground="White"
                BorderBrush="Yellow"
                BorderThickness="4"
                Margin="5,10,0,0"
                Padding="12,0,0,0"
                Click="ButtonEventHandler_Click"/>

        <Button Name="secondRowButton2"
                Grid.Column="1"
                Grid.ColumnSpan="1"
                Grid.Row="1"
                Content="Button 3"
                HorizontalAlignment="Right"
                HorizontalContentAlignment="Right"
                VerticalAlignment="Top"
                VerticalContentAlignment="Top"
                FontSize="52"
                FontWeight="Bold"
                FontStyle="Normal"
                Width="330"
                Height="150"
                Background="MediumVioletRed"
                Foreground="White"
                BorderBrush="Yellow"
                BorderThickness="4"
                Margin="0,10,5,0"
                Padding="0,0,12,0"
                Click="ButtonEventHandler_Click"/>

        <Button Name="thirdRowButton1"
                Grid.Column="0"
                Grid.ColumnSpan="1"
                Grid.Row="2"
                Content="Button 4"
                HorizontalAlignment="Left"
                HorizontalContentAlignment="Left"
                VerticalAlignment="Bottom"
                VerticalContentAlignment="Bottom"
                FontSize="52"
                FontWeight="Bold"
                FontStyle="Normal"
                Width="330"
                Height="150"
                Background="DarkCyan"
                Foreground="Gold"
                BorderBrush="Yellow"
                BorderThickness="4"
                Margin="5,0,0,5"
                Padding="12,0,0,0"
                Click="ButtonEventHandler_Click"/>

        <Button Name="thirdRowButton2"
                Grid.Column="1"
                Grid.ColumnSpan="1"
                Grid.Row="2"
                Content="Button 5"
                HorizontalAlignment="Right"
                HorizontalContentAlignment="Right"
                VerticalAlignment="Bottom"
                VerticalContentAlignment="Bottom"
                FontSize="52"
                FontWeight="Bold"
                FontStyle="Normal"
                Width="330"
                Height="150"
                Background="DarkCyan"
                Foreground="Gold"
                BorderBrush="Yellow"
                BorderThickness="4"
                Margin="0,0,5,5"
                Padding="0,0,12,0"
                Click="ButtonEventHandler_Click"/>

    </Grid>
 
</Window>

=================

MainWindow.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;

namespace ButtonAndGridControlWPFCS
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void FirstRowButton1_Click(object sender, RoutedEventArgs e)
        {
            // Ex.1
            Button btn = (Button)sender;
            MessageBox.Show("You pressed:" + Environment.NewLine +
                            "Button Name: " + btn.Name + Environment.NewLine + 
                            "Button Text: " + btn.Content.ToString(), "WPF (C#) : Button & Grid Example", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void ButtonEventHandler_Click(object sender, RoutedEventArgs e)
        {
            // Ex.2
            Button btn = (Button)sender as Button;
            MessageBox.Show("You pressed:" + Environment.NewLine +
                            "Button Name: " + btn.Name + Environment.NewLine +
                            "Button Text: " + btn.Content.ToString(), "WPF (C#) : Button & Grid Example", MessageBoxButton.OK, MessageBoxImage.Information);
        }

    }
}

Comments