Setting this value to Name will allow you to retrieve the name of the Color object and use that object to extract the Red, Green and Blue color values.Īfter you select a specific color from the list box, you click on the Get Color button on the window. The final piece of the list box control is the setting of the SelectedValuePath. A thin black border will be used around the Rectangle control to clearly define the color swatch. Each list item will use a label for the name of the color, and will set the Fill attribute of a Rectangle control to display the actual color. To accomplish this you use a DataTemplate and layout the display of each list item. For this list box, you want to not only have the name of the color displayed, but also a display of the actual color. Once you have the collection of Color objects you now bind them to the list box control. You call the GetProperties method on the data type in the first ObjectDataProvider to give you Color object collection to which you can bind to the ListBox control on the Window. The next ObjectDataProvider uses the first ObjectDataProvider as its source of data and then calls the GetProperties method on this souce.įor this second ObjectDataProvider the data is the instance of the data of the first ObjectDataProvider. The parameter in the MethodParameters is the exact string used in the code shown earlier. You do this by adding the following XAML within your Window declaration: You need to add the “sys” namespace to your Window object. In the above ObjectDataProvider you are creating an instance of the Type object located in the “sys” namespace. The first ObjectDataProvider you create looks like the following: This means you need to use two ObjectDataProviders. First, you call the GetType method, then you call GetProperties method. As you can see in the above code, you need to perform two operations to get the final collection. Since the name of the property is the name of the color, you bind the Name property to the list box.įrom the above code you can then figure out how to lay out your ObjectDataProvider, or in this case, two ObjectDataProvider objects. It is in the PropertyInfo object that you can get the Name property. Once you retrieve the Type, you call the GetProperites method on type to get a collection of PropertyInfo objects. For any class that is not in the System assembly, you need to use the full assembly qualification. You pass to this method the fully qualified class name including the namespace and the assembly information. In the above code, you first use the GetType method of the Type class. PublicKeyToken=31bf3856ad364e35").GetProperties() ĭim clrs As PropertyInfo() = Type.GetType( _ NOTE: This code is not used in this project, it is just here as an example of how to use reflection to get the list of properties of a Color object. Below is some sample code you might write to retrieve and print out each individual color name from the Colors class. Once you have the Name property you can bind that property to the list box shown in Figure 1. So once you have each Color object, you then need all of its properties so you can retrieve the Name property. Each property of the Colors class is a Color object. So you need to use reflection to get the collection of properties. To retrieve the WPF Colors is also a little tricky, as the Colors class contains individual static/Shared properties, one property to represent each color. Once I have that, I usually find it is very easy to convert that code to use an ObjectDataProvider. One thing that helps me immensely – and I hope will work for you - is to write a little code that will return a collection of items. You can get the complete list of Colors using an ObjectDataProvider object, however setting up the ObjectDataProvider can be a little tricky. The first thing this utility needs to do is to load all the WPF colors into a list box. In Figure 1 you can see a utility I wrote to help me do this.įigure 1: Utility to display all the WPF colors and retrieve the RGB values Thus, I needed a little utility that would help me get these numbers for the color I was looking for. Well, I don’t know about you, but I do not remember all of the (R)ed, (G)reen, and (B)lue color numbers off the top of my head. Thus, I need to create a Color resource that looks like the following: I can’t just create a string resource and put that into the Color attribute. Instead of hard-coding the color “Gray”, I would like to use a resource. This element has a “Color” attribute that you can set as shown in the code below: For example, I am using the on many of my controls. Instead of hard-coding colors, I use resources. During this project, I needed to set a lot of colors. Recently, I have been setting up a lot of WPF styles for an application I am working on.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |