TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

WPF DockPanel Example

This WPF example uses the DockPanel control to align sub-controls to the top, bottom, left or right of an area.

DockPanel. A DockPanel helps align controls to edges.

We add a control to it, and then can use the Dock property to align to the top, bottom, left or right. This makes the inner control "attach" to that edge.

Based on:

.NET 4.5

Example. This example will render similarly to the screenshot. A Window has a Grid, and in the Grid I added a DockPanel. I adjusted the HorizontalAlignment of the DockPanel to stretch across the width of the window.

HorizontalAlignment

Next: I dragged three Buttons to the DockPanel. I adjusted their heights and set their DockPanel.Dock attributes.

Dock: The DockPanel.Dock attribute handles four values: top, bottom, left and right. Only one can be specified.

I set the "Cat" button to be docked on the left. The middle button, "Dog," is left with the default: it sets against the Cat button. And the third button, "Squirrel" is docked to the right of the DockPanel.

Example markup: XAML

<Window x:Class="WpfApplication7.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>
	<DockPanel HorizontalAlignment="Stretch" LastChildFill="False" Margin="0"
		   VerticalAlignment="Top">
	    <Button Content="Cat" Height="100" VerticalAlignment="Top" Width="75"
		    DockPanel.Dock="Left"/>
	    <Button Content="Dog" Height="100" VerticalAlignment="Top" Width="75"/>
	    <Button Content="Squirrel" Height="100" VerticalAlignment="Top" Width="75"
		    DockPanel.Dock="Right"/>
	</DockPanel>
    </Grid>
</Window>

In the screenshot, we see that the buttons are attached to their respective edges of the DockPanel. And if you run a program like this one, try resizing the Window (by dragging an edge). The three Buttons will remain at the edges.

Tip: The HorizontalAlignment property of the DockPanel is important for this behavior. It makes the DockPanel expand to the window size.

 

Summary. Most WPF controls are not exciting. But together, they offer the ability to quickly construct useful programs. Many programs will have no need for a DockPanel. But this need occasionally arises.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf