First create a new window and then create a stack panel control by drag and drop from the toolbox within that window or write the following code for creating stack panel.
[csharp]<StackPanel Margin="0,0,0,-32"</StackPanel>[/csharp]
Now create a button controls in that stack panel control. For creating buttons you have to write the XMAL code like below:
[csharp]
<Window x:Class="WPF_Stackpanel.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
< Title="WPF | Stack Panel" Height="350" Width="525" Background="AliceBlue">
<Grid Background="BlueViolet">
<StackPanel Margin="0,0,0,-32">
<TextBlock Margin="198,10,10,10" FontSize="20" Stack Panel</TextBlock>
<Button Margin="115,10,109,10" Click="Button_Click" Background="BlanchedAlmond"/>
<Button Margin="115,10,109,10" Click="Button_Click_1" Background="BlanchedAlmond"/>
<Button Margin="115,10,109,10" Click="Button_Click_2" Background="BlanchedAlmond"/>
<Button Margin="115,10,109,10" Click="Button_Click_3" Background="BlanchedAlmond"/>
<Button Margin="115,10,109,10" Click="Button_Click_4" Background="BlanchedAlmond"/>
<Button Margin="115,10,109,10" Click="Button_Click_5" Background="BlanchedAlmond"/>
<Button Margin="115,10,109,10" Click="Button_Click_6" Background="BlanchedAlmond"/>
</StackPanel>
</Grid>
</Window>
[/csharp]
After writing the above code, the WPF Layout window should become like a below screen:
Now write the code for buttons click event in Main window.xmal.cs file.
[csharp]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPF_Stackpanel
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Welcome to C Language");
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
MessageBox.Show("Welcome to C++");
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
MessageBox.Show("Welcome to Csharp");
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
MessageBox.Show("Welcome to VB.NET");
}
private void Button_Click_4(object sender, RoutedEventArgs e)
{
MessageBox.Show("Welcome to ASP.NET");
}
private void Button_Click_5(object sender, RoutedEventArgs e)
{
MessageBox.Show("Welcome to WPF");
}
private void Button_Click_6(object sender, RoutedEventArgs e)
{
MessageBox.Show("Welcome to WCF");
}
}
}
[/csharp]
Now click
f5 button to run the application. After clicking f5 button, the below screen appears.