Event attributes need an event handler defined in a C# file. This video looks at the automatically added event handlers and verifies that they match the correct event signature. Next is a look at how to use the sender parameter to determine what UI element raised the event. Finally you’ll see how to use the special EventArgs parameter available in each event handler.
- [Instructor] I've added my event attributes to my XAML, and I'm ready to add some code in my C#. Before I do that, I would like to review a couple of items. One, remember that there are two buttons, on line 22 and line 23, that both have the same click handler. They both point to Button_Click. The second thing is remember that every time I added an event attribute, Visual Studios wrote a name for me. It suggested creating a new event handler, and it wrote the name. At the same time, over in the C# code, it created the method.
The nice thing that it did is it used the name that it suggested, and it used the correct signature. So the MouseMove event is raising the event, and it's going to pass two arguments over here. Visual Studio created a method with those two arguments. This first item represents the item that raised the event. The second one is event-specific information. In this case, the mouse event is going to hand me a bunch of data bundled inside this MouseEventArgs class, and then I can look through it and pull out the bits that I want to use in my code.
You'll get MouseEventArgs here, RoutedEventArgs here, SelectionChangedEventArgs here. You get the idea. There's different bunches of data that I get back depending on who's raising the event. I'll start by looking at this button. Remember, this is being called by two different buttons. On line 38, I'm getting the sender, which of the two buttons is raising the event, and I'm casting it to a button so I can work with it. I'll write an if statement here like that. That's verifying that I successfully got an instance of the button, I don't have a null reference.
I didn't do a bad cast. Then, I will put out a message. I have a TextBlock on my UI called MessageTextBlock. This gets the content, in other words the text that's on the button, in my case, and shows it to the user. Press F5 to run the application. Keep your eye down here in the lower left. Click on the button, and it's reading the text off each of those two buttons.
In the next demonstration, I'll look at the information you get in the EventArgs. In the MouseEventArgs, I get information about the mouse. So I get buttons. I can tell whether they clicked the right button, or the middle button, or the left button. I can find out whether they're using a stylus and tapping the screen. The information that I want is coming from this method called GetPosition. This allows me to find out the relative position of the mouse compared to the rest of the window.
Start by declaring a string variable and formatting the string. Then, these two placeholders here, I'll get the information from the MouseEventArgs, call GetPosition, and it expects an argument. What it expects is the relative object that I want the position compared to. So I could get the position compared to the upper left-hand corner of the rectangle or, for my example, I use the upper left-hand corner of the Window class.
Since I'm inside the Window class, I can use the 'this' keyword. I can come over here and do .X. That gets me the x-coordinate. Paste that in here. Get the y-coordinate, and then I'll output the information to the MessageTextBlock. That looks correct. Press F5 to debug the application. Now, as I move my mouse over the orange rectangle, I see the position change here in the text block.
The last example, I'll look at what happens when you select one of these items in the list box. I want to show the text for the selected item. That's very similar to what I did with the button code, but now I have to figure out how to get the currently selected item. Start by using this code. I'll say var item equal TourListBox.SelectedItem. It gets the selected item out of the current list box. If you look at my XAML, you'll see that each one of these items is wrapped in something called a ListBoxItem.
That's what I'm casting it to here. Then, once I have it cast successfully, I can build up a string. Like this. I'll say, "You'll love our," and then I get the current item, get its content.ToString. It's very similar to what I showed you in the button code down here on line 42. Paste that in there. Then, I'm ready to run the final demo. I'll select Barcelona.
You see I get the message, "You'll love our Barcelona tour." Then, you get the idea. Click in Singapore or Chicago, I get the same information. That brings this chapter to a close. We've seen how to work with object elements and their property and event attributes. For veteran .NET developers, XAML looks like another way of instantiating objects and assigning property values. Fundamentally, that's really the core of what XAML is.
- What is XAML?
- What frameworks use XAML?
- Working with XAML and Visual Studio
- Exploring XAML namespaces
- Instantiating objects
- Subscribing to events
- Using XAML in Windows Presentation Foundations, Universal Windows, and more