WPF Interaction - Windows Presentation Foundation(WPF)

What is WPF Interaction?

In WPF, an interaction show how an analysis interacts with controls located in that view. The most normally known interactions are of two types −

  • Behaviours
  • Drag and Drop


Behaviours were introduced with Expression Blend 3 which can encapsulate some of the functionality into a reusable constituent. To add additional behaviours, you can attach these components to the controls. Behaviours present more flexibility to design complex user interactions simply.

Let’s take a look at a simple instance in which ControlStoryBoardAction behaviour is attach to controls.

  • Create a new WPF project with the name WPFBehavior.
  • The following XAML code creates an ellipse and two buttons to control the movement of the ellipse.

When you accumulate and execute the above code, it will construct the following window which contains an ellipse and two buttons.
interaction_output1 (1)

When you press the play button, it will start moving from left to right and then will return to its unique position. The stop button will stop the association the ellipse.
interaction_output2 (1)

Drag and Drop

Drag and Drop on user interface can considerably advance the efficiency and productivity of the application. There are very a small number of applications in which drag and drop features are used because people think it is complex to implement. To a level, it is difficult to handle a drag and drop feature, but in WPF, you can handle it quite easily.

Let’s take a easy instance to understand how it works. We will create an application wherein you can drag and drop colour from one rectangle to another.

  • Create a new WPF project with the name WPFDragAndDrop.
  • Drag five rectangles to the design window and set the properties as shown in the following XAML file.

  • The first rectangle is the target rectangle, so the user can drag the color from the other rectangle to the target rectangle.
  • Given below is the events implementation in C# for drag and drop.

When you run your application, it will produce the following window.

drag_and_drop_output1 (1)

If you drag a colour from the rectangle on the right side and drop it on the large rectangle to the left, you will observe its effect immediately.
Let’s drag the 4th one from the right side.
drag_and_drop_output2 (1)

You can observe that the colour of the target rectangle has transformed. We suggest that you execute the above code and research with its description.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Windows Presentation Foundation(WPF) Topics