In this chapter we'll examine the UIElement base class. This video looks how UIElement defines members that add support for WPF essentials such as layout, input, focus and event handling.
- [Instructor] In this chapter, we'll examine the UIElement base class. As you can see from this class diagram, it is the base class for FrameworkElement, which in turn is the base class for all of the other useful elements, like the Control and Shape classes. We'll look at it in detail over here in this UIElement class diagram. The UIElement class add support for WPF essentials, such as layout, input, focus, and event handling. Let's take a look in the Events section.
It's here that the raw input information from the operating system, like mouse messages, are transformed into useful events like MouseMove and MouseEnter. Let's go down to the bottom of this list and start there. There is support for touch with these events and support for stylus, with a number of stylus events. There's also support for a number of mouse actions, like MouseDown and MouseEnter. And you'll notice there is specific mouse downs and up. There's MouseLeftButtonUp and down, and MouseRightButtonUp and down.
That way, you don't have to write branching code in your MouseDown event handler. There's also support for some basic KeyUp and KeyDown. At the top, you'll find that there is some drag events. So if you want to do a drag operation, there is some DragEnter, DragLeave, and DragOver. There's also the Drop event that happens when you drop something on a target. I'll show some examples of these events in this chapter.
In the Methods section, you'll find methods that support the layout engine. In WPF, it uses a two-pass layout process, the measure phase and the arrange phase. So you'll find a method called Measure and a method called Arrange. If you want to force an arrangement or force a measure, then you can use these methods, InvalidateMeasure and InvalidateArrange. You typically call these methods when you're creating your own custom control. It's not very often that you would use these when you're consuming the built-in controls that are part of the WPF framework.
There is also some methods here for capturing the mouse, stylus, and touch. Let's see what that means. We'll look at Class Details and I'll pin this to the bottom of the window. So this says, "Attempts to force the capture "of touch to this element," or forced to capture the mouse to this element. There are times when you need to tell the WPF engine to ignore all the other elements on the page and attempt to capture the mouse just for this particular element. This is also a place where you can call the Focus method, which attempts to set the input focus to this element.
There's also a method here called BeginAnimation, which starts an animation for any specified animated property on this element. Next, we'll look at Properties. There's a lot of useful properties in here. For instance, there's the AllowDrop. If you're working with drag and drop operations, this is how an element opts in and says it's okay to drop items on this element. In the WPF world, the rendering engine's really rendered with DirectX, so there is a property here called Effect that allows me to apply an effect to this element.
WPF ships with a couple of default effects like the blur effect and the drop shadow, but you can also create your own custom effects and assign them to this Effect property. I mentioned earlier that the UIElement is where you find some of the layout features. So you'll find in here, there's a DesiredSize property, which is the size that this control, this element tells the WPF framework that it wants to be and then the framework will decide whether or not it will honor those settings.
There's also a RenderSize property that will then tell you what the current rendered size is of this element. There's also a lot of Is Boolean properties like IsEnabled and IsHitTestVisible and IsMouseOver. These are interesting. You may recall that they were mouse events, so there's a mouse event that fires when you mouse into an element, and so you can write code that fires on that. But at the same time, the mouse enters that element, it also switches this Boolean property to true.
So IsMouseOver is true, and when you mouse away from that element, this property, it will be set to false. And this allows you to test in your code whether the mouse is over it or more importantly, it allows us to do things like triggers, where we can set up trigger conditions that say, "If this condition is true, "then change the UI to look like this." This is how you get the colors of the background, on the fam, on say a slider, to change by having a trigger on the control itself. What else is interesting? How about the Uid property? Let's see what that says.
"Getter sets the unique identifier "for localization of this element." So if you want to localize the text that is on a button or inside a menu item, then you want to assign a Uid property here and then that will be used to retrieve the correct localized string and apply it to the UI at runtime. Finally, there is the Visibility and the Opacity property. Rest of this chapter looks at examples of the UIElement's properties and events.
- Interactions and controls
- UIElement family
- FrameworkElement family
- Shape family
- Decorator Family
- Control Fundamentals
- Button family
- Text Editor family