TOC

This article is currently in the process of being translated into Greek (~36% done).

XAML:

Συμβάντα (Events) στην XAML

Τα περισσότερα μοντέρνα πλαίσια Διεπαφών (UI) βασίζονται στην ανταπόκριση σε συμβάντα (events). Όλα τα controls περιλαμβανομένου του Παραθύρου -Window- (το οποίο επίσης κληρονομεί τις ιδιότητες της Control Class) εκθέτουν για σειρά από συμβάντα (events) στα οποία μπορεί να γίνει εγγραφή για ειδοποίηση. Να γίνει εγγραφή για ειδοποίηση στα συμβάντα, σημαίνει ότι η εφαρμογή σου θα ειδοποιηθεί όταν αυτά συμβούν ώστε να κάνει μια ενέργεια.

Υπάρχουν πολλοί τύποι από συμβάντα, αλλά μερικά από αυτά τα οποία χρησιμοποιούνται ποιο συχνά είναι αυτά που οφείλονται στην αλληλεπίδραση του χρήστη με την εφαρμογή χρησιμοποιώντας το ποντίκι ή το πληκτρολόγιο. Τα περισσότερα controls έχουν συμβάντα όπως KeyDown (ΠλήκτροΚάτω) , KeyUp (πλήκτροΠάνω) , MouseDown (ΠοντίκιΚάτω), MouseEnter(ΠοντίκιΕισέρχεται) , MouseLeave (ΠοντίκιΦεύγει), MouseUp (ΠοντίκιΠάνω) και μερικά άλλα.

Θα δούμε πιο αναλυτικά πώς λειτουργούν τα συμβάντα στο WPF, καθώς αυτό είναι ένα πολύπλοκο θέμα, αλλά προς το παρόν, πρέπει να ξέρετε πώς να συνδέσετε ένα control event (συμβάν) στην XAML με ένα τμήμα κώδικα του αρχείου Code-behind. Ρίξτε μια ματιά σε αυτό το παράδειγμα:

<Window x:Class="WpfTutorialSamples.XAML.EventsSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="EventsSample" Height="300" Width="300">
	<Grid Name="pnlMainGrid" MouseUp="pnlMainGrid_MouseUp" Background="LightBlue">        
		
    </Grid>
</Window>

Παρατηρήστε πως δημιουργήσαμε εγγραφή στο συμβάν MouseUp (ΠοντίκιΕπάνω) ενός Grid (πλαίσιο Δικτύου) αναγράφοντας το όνομα μιας μεθόδου. Αυτή η μέθοδος πρέπει να οριστεί στον κώδικα του αρχείου Code-Behind, χρησιμοποιώντας την σωστή υπογραφή συμβάντος. Στην συγκεκριμένη περίπτωση θα πρέπει να μοιάζει ως ακολούθως:

private void pnlMainGrid_MouseUp(object sender, MouseButtonEventArgs e)
{
	MessageBox.Show("You clicked me at " + e.GetPosition(this).ToString());
}

The MouseUp event uses a delegate called MouseButtonEventHandler, which you subscribe to. It has two parameters, a sender (the control which raised the event) and a MouseButtonEventArgs object that will contain useful information. We use it in the example to get the position of the mouse cursor and tell the user about it.

Several events may use the same delegate type - for instance, both MouseUp and MouseDown uses the MouseButtonEventHandler delegate, while the MouseMove event uses the MouseEventHandler delegate. When defining the event handler method, you need to know which delegate it uses and if you don't know that, you can look it up in the documentation.

Fortunately, Visual Studio can help us to generate a correct event handler for an event. The easiest way to do this is to simply write the name of the event in XAML and then let the IntelliSense of VS do the rest for you:

When you select <New Event Handler> Visual Studio will generate an appropriate event handler in your Code-behind file. It will be named <control name>_<event name>, in our case pnlMainGrid_MouseDown. Right-click in the event name and select Navigate to Event Handler and VS will take you right to it.

Subscribing to an event from Code-behind

The most common way to subscribe to events is explained above, but there may be times where you want to subscribe to the event directly from Code-behind instead. This is done using the += C# syntax, where you add an event handler to event directly on the object. The full explanation of this belongs in a dedicated C# example, but for comparison, here's an example:

using System;
using System.Windows;
using System.Windows.Input;


namespace WpfTutorialSamples.XAML
{
	public partial class EventsSample : Window
	{
		public EventsSample()
		{
			InitializeComponent();
			pnlMainGrid.MouseUp += new MouseButtonEventHandler(pnlMainGrid_MouseUp);
		}

		private void pnlMainGrid_MouseUp(object sender, MouseButtonEventArgs e)
		{
			MessageBox.Show("You clicked me at " + e.GetPosition(this).ToString());
		}

	}
}

Once again, you need to know which delegate to use, and once again, Visual Studio can help you with this. As soon as you write:

pnlMainGrid.MouseDown +=

Visual Studio will offer its assistance:

Simply press the [Tab] key twice to have Visual Studio generate the correct event handler for you, right below the current method, ready for implementation. When you subscribe to the events like this, you don't need to do it in XAML.