Step 1: Create an empty WPF using Visual Studio, enter the name for the application and click on OK
Step 2: Add elements with help of Stackpanel.
Code:
- <StackPanel Margin="-10,0,9.667,-0.333">
- <Button Name="Hi" Margin="45,45,0,0" Content="Name" />
- <Button Name="hw" Margin="45,45,0,0" Content="Name" />
- <Button Name="r" Margin="45,45,0,0" Content="Name" />
- <Button Name="u" Margin="45,45,0,0" Content="Name" />
- </StackPanel>
Code of Mainwindow.xaml:
- <Window x:Class="Panels_WPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525">
- <Grid>
- <StackPanel Margin="-10,0,9.667,-0.333">
- <Button Name="Hi" Margin="45,45,0,0" Content="Name" />
- <Button Name="hw" Margin="45,45,0,0" Content="Name" />
- <Button Name="r" Margin="45,45,0,0" Content="Name" />
- <Button Name="u" Margin="45,45,0,0" Content="Name" />
- </StackPanel>
- </Grid>
- </Window>
Step 3: Add a new window by right clicking on the solution name and name it as Wrapapnel.xaml for Wrappanel
Code of Wrappanel.xaml:
- <Window x:Class="Panels_WPF.Wrappanel" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Wrappanel" Height="300" Width="300">
- <Grid>
- <WrapPanel>
- <Button Name="Hi" Margin="45,45,0,0" Content="Name" />
- <Button Name="hw" Margin="45,45,0,0" Content="Name" />
- <Button Name="r" Margin="45,45,0,0" Content="Name" />
- <Button Name="u" Margin="45,45,0,0" Content="Name" />
-
- </WrapPanel>
-
- </Grid>
- </Window>
The window will be as such for wrappanel,
Step 4: Now make the button at Mainwindow.xaml for Wrappanel to open this window using the following code:
Double click on Wrap panel button and write the following code at Mainwindow.xaml.cs
- Wrappanel p = newWrappanel();
- p.Show();
Code for mainwindow.xaml.cs:
- using System;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Threading.Tasks;
- usingSystem.Windows;
- usingSystem.Windows.Controls;
- usingSystem.Windows.Data;
- usingSystem.Windows.Documents;
- usingSystem.Windows.Input;
- usingSystem.Windows.Media;
- usingSystem.Windows.Media.Imaging;
- usingSystem.Windows.Navigation;
- usingSystem.Windows.Shapes;
- namespacePanels_WPF
- {
-
-
-
- publicpartialclassMainWindow: Window
- {
- publicMainWindow()
- {
- InitializeComponent();
- }
- privatevoidHi_Click(object sender, RoutedEventArgs e)
- {
- Wrappanel p = newWrappanel();
- p.Show();
- }
- }
- }
Step 5: Create a new window the same for Dockpanel and place the elements within the grid using. <Dockpanel></DockPanel>.
The code should be as:
- <Window x:Class="Panels_WPF.Dockpanel" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Dockpanel" Height="300" Width="300">
- <Grid>
-
- <DockPanel>
- <Button Name="Hi" Margin="45,45,0,0" Content="Name" />
- <Button Name="hw" Margin="45,45,0,0" Content="Name" />
- <Button Name="r" Margin="45,45,0,0" Content="Name" />
- <Button Name="u" Margin="45,45,0,0" Content="Name" />
- </DockPanel>
- </Grid>
- </Window>
Step 6: Now run the code: