Introduction to WPF Custom Controls is an introductory course designed to teach the basic concepts required to get started writing custom WPF controls. Learn how to choose a base class to start your custom control. Enable data binding support by adding your own custom properties. Learn how to add custom events and create custom commands. Add visual behavior to your custom control with the use of triggers and the visual state manager. You'll also learn how to add theming support to your custom control.
Brian Lagunas is a Microsoft MVP, a Microsoft Patterns & Practices Champion, Director of Technology for INETA, co-leader of the Boise .Net Developers User Group (NETDUG), board member of Boise Code Camp, speaker, trainer, author, and original creator of the Extended WPF Toolkit.
Introduction Hello and welcome to the "Introduction to WPF Custom Controls". My name is Brian Lagunas and, if you've ever had the need for control that Microsoft doesn't give you out of the box or that you don't want to pay for or a third party vendor doesn't provide to you, then this course is for you. Throughout this course, I'm going to teach you the basic concepts that you need to know in order to build custom WPF controls that fit your requirements. In this module, we're going to start by taking a look at the anatomy of a UserControl and seeing how that compares to the anatomy of a Custom Control. We're also going to discuss when you would want to use a UserControl versus when you would want to take the time to create your own Custom Control. And, lastly, we're going to finish this module by seeing how we can use these controls that we create in our own views.
Creating Custom Controls Hello, this is Brian Lagunas and in this module we're going to be discussing how to create a Custom Control. Now, I know what you're probably thinking. You're thinking, I know how to create a Custom Control. We just saw how to do it in the intro. I go File, Add New Item, Create Custom Control, and then we're off and running. Although that's true, there are some things you have to take into consideration before creating a Custom Control. And, in this module, we're going to discuss some of those items, such as choosing the right base class. Choosing the wrong base class can cause a lot of pain and headache for you. But if you choose the right one, you'll be on your way much more quickly. We're going to discuss what presenters are and what role they play in your control templates. We're going to go into data binding and how you data bind to properties within your control template. And, then we're going to discuss how to access template elements from within the visual tree of your Custom Control.
Adding Properties Hello, this is Brian Lagunas and in this module we're going to be talking about adding properties. So, in the previous modules we've already kind of seen, you know, what a Custom Control is all about, how we start creating it. We saw the control templates and the styles. But, you know, chances are your controls are going to need to maintain some type of state and when that state changes, various aspects or visual changes are going to occur on your Custom Control. So, we're going to use properties to control that type of behavior. So, what we want to do is we're going to talk about DependencyProperties. We're going to talk a little bit about how they're different than normal CLR properties. We're going to see what the property metadata is all about. It gives you some control over how the property behaves, if it affects render, things like that. We're going to discuss a little bit about read-only properties and where those would be useful. And, I'm just going to point out a gotcha when dealing with collection type properties. There's something you need to know just to kind of keep yourself out of trouble. And, then we're going to wrap this module up, talking about attached properties. Given that this is an introduction course, I'm not going to go into a ton of detail on DependencyProperties, but I am going to cover the basic concepts you're going to need to know in order to be proficient at adding properties and behavior and state management to your Custom Controls. So, let's go ahead and jump right in.
Events and Commands Hello, this is Brian Lagunas. So far in the course, we've learned how to create our Custom Controls, we've learned how to add custom properties to our controls. Now, it's time that we start adding events and commands to our controls. So, in this module, we're going to start by getting a good understanding of what RoutedEvents are. This is a Custom Control course, so I'm not going to go into a ton of detail on RoutedEvents, but I'm going to go deep enough to where we kind of understand what type of events we want to start creating. Then, we're going to look at how we would actually create our own custom RoutedEvents. Then, we're going to follow that up by understanding how routed commands work in WPF. And, then we'll start creating our own custom routed commands and implementing a commanding-type functionality into our own Custom Controls. So, let's go ahead and get started.
Visual Behavior Chances are, your Custom Control is not going to have some static UI, a UI that never changes based on any type of interaction, right? I'm pretty confident that your control's going to have to respond to some type of state changes or some type of user interaction. That may be a mouse click, changing the background color, or when the mouse enters or leaves or clicks a certain area of your control, certain aspects of your control will change in response to that. That's what we refer to as visual behavior. Hi, this is Brian Lagunas and in this module, I'm going to be discussing the various ways you can add a visual behavior to your Custom Control. And, in WPF, there's two main ways to achieve visual behavior. The first one are triggers. The next one is the VisualStateManager. And, at the end of this module, we're going to be talking about the triggers versus the VisualStateManager argument. Hopefully, we can look at some of the pros and cons of each and you can make the decision on which one is best for you implementation. So, let's go ahead and get started.
Theming We know how to create a control, we know how to add properties to it, add custom events and commands. We even know how to style our control and add visual behavior or triggers in the VisualStateManager. Well, now it's time to take our Custom Control to the next level by adding theming support. Hello, this is Brian Lagunas and this module, it's all about theming. We're going to start out by talking about what theming is. And, honestly, it's not complicated. But, there are some concepts you should understand before we really dive into theming or implementing themes for your Custom Control. These include styles, ResourceDictionaries, using things like the static resource markup extension and the dynamic resource markup extension. Once we get through what theming is, we're going to look at how easy it is to actually create a theme. Theming is one of the most misunderstood concepts in WPF and a lot of people think it's more complicated than it really it. Well, we're going to find out just how easy it is to implement theming for your Custom Controls. Then, we're going to look at naming conventions. We're going to look at the naming conventions required by WPF in order to get theming to work properly for your Custom Control. Then, we're going to look at some helpful attributes that we can use on our Custom Control assembly that will make your control more useful within XAML, as well as make it easier on developers consuming your control. And, lastly, we're going to end this module by just discussing a couple of tips that I've discovered through my years of writing Custom Controls and theming files that hopefully will help you when you start getting involved in writing theming support for your Custom Control. So, let's go ahead and get started.