![]() Here is a more advanced way of changing the color of the shape in C# code. MySolidColorBrush.Color = (Color)ColorConverter.ConvertFromString("DarkSlateGray") We want to use C# code to change the color of the arrow as we did in the above listing, but this time we want to use the color name “DarkSlateGray”. MySolidColorBrush.Color = Color.FromArgb(255, 0, 150, 255) Īs a demo, let’s go back to the original dark slate gray. Describes the brush's color using RGB values. SolidColorBrush mySolidColorBrush = new SolidColorBrush() Create a SolidColorBrush with a blue color to fill the triangle with Below is the code you can use to do this. If you want to change that in C# code, you first need to create a solid color brush. Since the color is set in XAML, when the program starts it will display as a dark-grey color. Here below is some code to display a small gray triangle pointing to the right, which could be your “play” or “forward” button. If you are working with XAML and all you need to do is set the color of a shape, for example, it is very easy. Object.Color = (Color)ColorConverter.ConvertFromString("Blue") Replace the string “Blue” and replace the object to your specific named object. If you find the need to convert a color string such as Blue, Red, Cyan, Magenta, Azure, and so on to a Color, you could try the code below, modifying it to suit your needs. The Color of the SolidColorBrush is set to Blue and the Opacity is set to 0.5.”Ĭonvert Color String to a Color in C# Code The next example creates a Rectangle and explicitly creates the SolidColorBrush as an element value for a Rectangle.Fill property. The brush being created is created implicitly and automatically, as part of a deliberate XAML language shorthand that helps keep UI definition simple for the most common cases. In the previous examples, you never even saw the string “SolidColorBrush” appear in the syntax. For more info on XAML syntax, including property element syntax, see XAML overview and XAML syntax guide. This syntax is more verbose than the previous methods, but you can specify additional property values on an element, such as the Opacity. Microsoft says: “You can use property element syntax to define a SolidColorBrush. Here, FF is the largest hexadecimal number you can have, while 00 is the lowest. For example, the hexadecimal value “#FFFF0000″ defines fully opaque red (alpha=”FF”, red=”FF”, green=”00″, and blue=”00″). Two characters in the range 0 to F define each component value, and the component value order of the hexadecimal string is: alpha channel (opacity), red channel, green channel, and blue channel (ARGB). You can use a hexadecimal format string to declare precise 24-bit color values with 8-bit alpha channel for a SolidColorBrush. Feel free to use this code to create your own Windows project. The 256 named colors are based on the X11 color names from the Cascading Style Sheets, Level 3 (CSS3) specification, so you may already be familiar with this list of named colors if you have previous experience with web development or design.” We have a Windows Presentation Foundation project at this site that you can use to practice with. The XAML parser converts the color name to a Color structure with the correct color channels. Microsoft says: “You can use a predefined color name, such as Yellow or Magenta. There are three ways in XAML to define a SolidColorBrush and the solid color it specifies: predefined color names, hexadecimal color values, or the property element syntax”. Microsoft in their website article Using brushes to paint backgrounds, foregrounds, and outlines says: “A SolidColorBrush paints an area with a single Color, such as red or blue.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |