ProfK
ProfK

Reputation: 51084

How do I get a value of Windows.Media.Colors using a string?

I want to take a string like "Green" and get the Color type property of Colors that is named "Green", but I just can't figure how.

InvokeMember requires an object instance to invoke the named member on, but all the Color properties of Colors are static, and not available on an instance of Colors.

Upvotes: 2

Views: 127

Answers (1)

Kamil Budziewski
Kamil Budziewski

Reputation: 23107

Is it what you need?

var color = (Color)ColorConverter.ConvertFromString("Green");

it gets color by name using ColorConverter from Windows.Media. It is equivalent to:

var color = Color.Green;

Upvotes: 1

Related Questions