Layouts and Panels - lucyberryhub/WPF.Tutorial GitHub Wiki
Hey cupcake! π Are you ready to design your appβs layout so itβs as sweet and fun as a berry tart? π§ WPF has some magical layouts that help you organize your UI just like a berry farm full of delicious rows, stacks, and docks. Letβs dive into them and create something berry beautiful! π
Weβve got a variety of layouts in WPF, each with its own charmβjust like a different berry! πβ¨ Whether youβre looking to divide your space into neat sections, stack items, dock things to the edges, or place them anywhere like a free-spirited berry patch, WPF has got you covered. πΈ
The Grid is the queen of layouts, just like the biggest and juiciest berry in the patch! π With the Grid, you can divide your app into rows and columnsβso neat and tidy, like a berry waffle! π§β¨
-
How it works: The Grid uses
RowDefinitions
andColumnDefinitions
to create structured rows and columns. You can then place your elements anywhere in the grid by setting the Row and Column properties. So flexible, like a fresh berry tart with layers!
<Window x:Class="BerryFarmApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Berry Farm" Height="350" Width="525">
<Grid>
<!-- Defining Rows -->
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/> <!-- Top row with dynamic height -->
<RowDefinition Height="*"/> <!-- Bottom row takes the remaining space -->
</Grid.RowDefinitions>
<!-- Assign elements to specific rows -->
<TextBlock Text="Top Row π" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20"/>
<Button Content="Bottom Row π" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20"/>
</Grid>
</Window>
Berry Vibe: The Top Row is like the fresh berries on top, sized based on its content. The Bottom Row stretches out to fill the space left behind, just like those juicy berries at the bottom of your bowl. ππ
The StackPanel is perfect for stacking your elementsβlike a delicious pile of pancakes with berries on top! π₯π It arranges your elements either vertically or horizontally.
- How it works: You can stack elements either vertically (like a berry tower) or horizontally (like berries in a row). Itβs perfect for simple layouts where you want things to just fall in line! π§β¨
<Window x:Class="BerryFarmApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Berry Farm" Height="350" Width="525">
<StackPanel Orientation="Vertical">
<TextBlock Text="Stack those berries! π" HorizontalAlignment="Center" FontSize="20"/>
<Button Content="Another berry! π" HorizontalAlignment="Center" FontSize="20"/>
</StackPanel>
</Window>
Berry Vibe: Just like stacking those fresh berries one by one, the TextBlock and Button will be stacked one over the other when set to Orientation="Vertical"
. Change it to Horizontal
to stack them side by side like a row of berry baskets. π₯π
The DockPanel is here to help you dock your items to the edges of your layout, just like berry baskets placed at different spots around your farm! π§Ίπ
-
How it works: You can dock elements to the top, bottom, left, or right. Thereβs also a
Fill
option, which fills the remaining space with your content, like letting the berries spread out across the whole farm! πΈπ
<Window x:Class="BerryFarmApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Berry Farm" Height="350" Width="525">
<DockPanel>
<!-- Dock to the top -->
<TextBlock Text="Berry Top Basket π" DockPanel.Dock="Top" HorizontalAlignment="Center" FontSize="20"/>
<!-- Dock to the left -->
<Button Content="Berry Left Basket π" DockPanel.Dock="Left" VerticalAlignment="Center" FontSize="20"/>
<!-- Fill remaining space -->
<TextBlock Text="Berries in the Center π" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20"/>
</DockPanel>
</Window>
Berry Vibe: The TextBlock will sit at the top of the window (just like the first berry basket you pick), the Button is docked to the left side (ready for some berry picking), and the last TextBlock fills the rest of the space in the middle. ππ
The Canvas layout lets you position your elements absolutely, just like placing wild berries anywhere in the garden! π±π You can control exactly where each item goes, using X and Y coordinates.
- How it works: Use Canvas.Left and Canvas.Top to position elements with absolute precision. Itβs the most free-spirited layout, just like letting the berries fall where they may! π
<Window x:Class="BerryFarmApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Berry Farm" Height="350" Width="525">
<Canvas>
<TextBlock Text="Berry Patch!" Canvas.Left="50" Canvas.Top="30" FontSize="20"/>
<Button Content="Pick Berries π" Canvas.Left="100" Canvas.Top="100" FontSize="20"/>
</Canvas>
</Window>
Berry Vibe: With Canvas, you can place the TextBlock and Button wherever you want in your window, just like setting berries all over the farm. The TextBlock is positioned at (50, 30)
and the Button at (100, 100)
. Place them wherever you feel the berry vibes! πβ¨
Now youβve got the power of four awesome layout panels in your hands: Grid, StackPanel, DockPanel, and Canvas! Experiment with them, combine them, and create a layout thatβs as sweet as your berry farm! ππ
With these Berry Vibe layouts, your app will be as organized and adorable as a patch full of fresh fruit! πβ¨