0

The bigger question here is whether UWP has a mechanism to facilitate C# code interpreting/parsing a XAML string value to determine an underlying DependencyProperty value in the same manner Brush properties like Fill="" magically managed to do.

Has anyone figured out whether the mechanisms that allows a SolidColorBrush to be defined using various string formats ("Red", "Green", "#11223344", etc) can be reproduced in C# code for our own DependencyProperty object types?

kburgoyne
  • 181
  • 1
  • 2
  • There is a set of conversion mechanism inside the color, but we don't know how it is implemented. So if you want to use Hex to assign a Value to an Integer DependencyProperty, you can only achieve it manually. – Faywang - MSFT Dec 23 '19 at 07:59
  • Thanks, Faywang. Thus this issue is really "how" could one do it manually. How might one make the type of the DependencyProperty be "int" (or similar) but yet allow the person writing the XAML to specify "#11223344". That's the "trick" properties like "Fill" are performing. They're accepting essentially a string for conversion when the underlying DependencyProperty is a type like "Brush". – kburgoyne Dec 24 '19 at 17:37
  • For Brush, it allows predefined color names and hex to set value, since the XAML parser will convert the color name to a Color structure with the correct color channels or ARGB to display, but if you want to pass hex to an integer value, you need to convert manually. For example, when you get the hex string value, you could convert it to int value in code-behind and then pass it to your integer DependencyProperty. – Faywang - MSFT Dec 25 '19 at 07:09
  • @kburgoyne Your question was unreasonable. If you define an 'int' dependencyProperty and want to specify a string value like "#FFFFFF" to it on the xaml, it's impossible. You will get error information from the visual studio. You need to specify a string value that the xaml rendering engine can convert it to your dependency property type at least. For example, you could define a SolidColorBrush dependency property and specify string value like '"Red", "Green", "#11223344"' to it. – Xie Steven Dec 25 '19 at 09:47
  • public SolidColorBrush Fill { get { return (SolidColorBrush)GetValue(FillProperty); } set { SetValue(FillProperty, value); } } // Using a DependencyProperty as the backing store for Fill. This enables animation, styling, binding, etc... public static readonly DependencyProperty FillProperty = DependencyProperty.Register("Fill", typeof(SolidColorBrush), typeof(MyUserControl1), new PropertyMetadata(0)); – Xie Steven Dec 25 '19 at 09:47

1 Answers1

0

You could use the Colors Helper class in Windows Community Toolkit. It lets users convert colors from text names, HTML hex, HSV, or HSL to Windows UI Colors (and back again of course).

using Microsoft.Toolkit.Uwp.Helpers;

// Given an HTML color, lets convert it to a Windows Color
Windows.UI.Color color = ColorHelper.ToColor("#3a4ab0");

// Also works with an Alpha code
Windows.UI.Color myColor = ColorHelper.ToColor("#ff3a4ab0");

// Given a color name, lets convert it to a Windows Color
Windows.UI.Color redColor = "Red".ToColor();
Xie Steven
  • 8,544
  • 1
  • 9
  • 23