-
Book Overview & Buying
-
Table Of Contents
Windows Presentation Foundation 4.5 Cookbook
By :
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.
Make sure Visual Studio is up and running.
We'll create a simple circle drawing program. Every click adds a circle to a Canvas.
Create a new WPF application named CH03.PaintingCircles.
Open MainWindow.xaml. Replace the default Grid with a Canvas, name it _canvas, and set its Background to White.
Add an event handler for the MouseUp event to the Canvas:
<Canvas x:Name="_canvas" Background="White"
MouseUp="OnClickCanvas">
</Canvas>Navigate to the event handler you just created. We want a left-click to add an ellipse where the mouse pointer is and a right-click to make an Ellipse under the cursor disappear.
Add the following code to the event handler in case...