Placing elements in exact positions
WPF's layout panels provide much flexibility in building a user interface. Furthermore, most of the panels adapt well to size and content changes of themselves and their child elements. This means that positions and sizes don't necessarily remain the same as the user resizes or otherwise manipulates the UI.
Most of the time that's a good thing; sometimes, however, we need to place elements in exact positions without them moving unexpectedly. This is typical of highly graphical content, such as graphs, charts, or animations. The Canvas
panel provides a convenient container for such content.
Getting ready
Make sure Visual Studio is up and running.
How to do it...
We'll create a simple application that hosts a Canvas
with some elements inside to demonstrate the way elements are positioned inside a Canvas
:
Create a new WPF application named
CH03.CanvasDemo
.Open
MainWindow.xaml
. Replace the existingGrid
with aCanvas
.Add some elements to the
Canvas
as follows:<...