From the course: Microsoft XAML: 2 Content and Properties

Use some dependency properties

From the course: Microsoft XAML: 2 Content and Properties

Start my 1-month free trial

Use some dependency properties

- [Instructor] Now that we've looked at some of the concepts of dependency properties, let's see how to use them. I'll be working with this TextBlock class. I'll right click on it, and choose Go To Definition. This will allow me to look up the different properties that are part of this class. Currently, this list over here, in the middle of the page, is sorted by Member Types. That means that all of the properties are sorted together, all of the methods are sorted in a different area, and so on. So, this area shows the .NET Properties. Below that, you can see the dependency properties. I'm going to sort these in alphabetical order. Then, I'll look at these properties in my demonstration. I'll look at FontSize, and FontWeight. Now that they're sorted alphabetically, I can see the .NET property, here, and the dependency property, indeed, below that. You can see one of the naming conventions that Microsoft uses here, for dependency properties, they always put the word Property on the end of the property name. You can also see the difference, by looking in this area. This is the Declaration. So, if I click on FontSize, you'll see that it says public double FontSize, and it has a getter and setter. So, it's a normal .NET property. If I click on the FontSizeProperty, this dependency property, you'll see that it uses different syntax. Public static readonly, property called FontSizeProperty, and then it's of type DependencyProperty. I'll talk more about how you create your own custom dependency properties later in this chapter. I'll be working with FontSize, and FontWeight. To set them in your XAML, return back to my XAML file, you just need to use the property name here, in the XAML. Use the short name. Like that. When I set the FontWeight, I get a drop down of choices. So, there is more than one choice for me, here, and I get a list. This is coming from the intellisense that Microsoft provides, in the XAML editor. I'll set this to Bold. Some items, like the Background, also give you a drop down. In this case, I can choose a named color. Like that. But they also support other types of information. For instance, I can use a hexadecimal value. #336633, like that, and I get a green color. Oh, and I should probably put some text in there, so we can see it, too. Use the Text Property. As you can see, it's very simple to use. We've seen it in other movies, in this chapter. You just use what's called a Property Attribute to set that. And, then, this maps to the short name, or the .NET property name. In order to work with it in code, let me switch over to the C# file, here. If you want to set the dependency property, you need to go to the instance of the type. In this case, it's called TextBlock1. And, you're going to use a special method, called SetValue. And this is how you set dependency properties. This is how you go directly to the dependency system, and set the value. So, you say SetValue. You're going to set the value for this instance, of this TextBlock. And, remember, who's managing the memory, right? The dependency system's managing this memory. So, I'll go to the SetValue. That tells the dependency system I've got some data. And, there's two arguments here. The dependency property that you want to set, and the new value. So, I want to set the FontSize. And, I'm going to choose the dependency property, not the .NET property. To read the value, declare a variable. And the syntax is nearly identical. It's TextBlock1.GetValue From here, you pass in one argument, the dependency property that you want to retrieve. Now, think about this for a second. The TextBlock has a number of dependency properties. So, I'm asking it to find the one that's registered, find the one, the value that's stored in the dependency system, and get that value. There you go. That's how you do it. I've also said, that most people that write dependency properties, also write .NET wrappers. So, an alternative way of saying this, in C# code, would be, to go to the instance of the object. Go directly to the .NET property. And set it. You want to hazard a guess how you would read that information, using the .NET property? Sure. You already know this one. That's the basics of reading, and writing the information in the code behind, and, also, setting the value, in the XAML file. Next up, I'll look at how the dependency system decides the order of precedence, when you have more than one item setting the dependency property, who wins, and how does it make that choice?

Contents