O'Reilly logo

Windows Presentation Foundation 4.5 Cookbook by Pavel Yosifovich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding/removing elements to a panel dynamically

Panels contain elements that are typically added in XAML as a base for the user interface. Sometimes, however, we need to add or remove elements dynamically at run time based on user actions or other criteria. Let's see how this can be done.

Getting ready

Make sure Visual Studio is up and running.

How to do it...

We'll create a simple circle drawing program. Every click adds a circle to a Canvas.

  1. Create a new WPF application named CH03.PaintingCircles.
  2. Open MainWindow.xaml. Replace the default Grid with a Canvas, name it _canvas, and set its Background to White.
  3. Add an event handler for the MouseUp event to the Canvas:
    <Canvas x:Name="_canvas" Background="White"
    MouseUp="OnClickCanvas">
    </Canvas>
    
  4. Navigate ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required