Let's add a Canvas panel inside the window, and dynamically add squares at the current cursor position when the user clicks the Canvas panel. Perform the following steps:
- Open the MainWindow.xaml page and replace the default Grid panel with a Canvas.
- Give it a name. In our example, let's give the name as canvasPanel.
- Set a background to the canvas panel and register a MouseLeftButtonDown event to it. Here's the complete XAML code, for reference:
<Window x:Class="CH03.DynamicPanelDemo.MainWindow" xmlns=
"http://schemas.microsoft.com/winfx/
2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Dynamic Panel Demo" Height="300" Width="500"> <Canvas x:Name="canvasPanel" Background="LightGoldenrodYellow" MouseLeftButtonDown="OnMouseLeftButtonDown"/>...