0

How can I set the property for a XAML control using a string variable?

This is an example of the variable to be used.

string imageStretch = "Uniform";

How can it be set like this programmatically in the code behind?

myImage.Stretch = imageStretch;

The reason is that I would like to avoid using a long chunk of code like this.

if (imageStretch == "None") { myImage.Stretch = Stretch.None; }
if (imageStretch == "Fill") { myImage.Stretch = Stretch.Fill; }
if (imageStretch == "Uniform") { myImage.Stretch = Stretch.Uniform; }
if (imageStretch == "UniformToFill") { myImage.Stretch = Stretch.UniformToFill; }

If this can be done, can it be done for other types of controls/properties as well?

theMaxx
  • 3,756
  • 2
  • 27
  • 33
  • Possible duplicate of [How do I convert a string to an enum in C#?](http://stackoverflow.com/questions/16100/how-do-i-convert-a-string-to-an-enum-in-c) – dymanoid Nov 16 '16 at 21:03

1 Answers1

1

use enum.Parse() like such...

myImage.Stretch = (Stretch)Enum.Parse(typeof(Stretch), imageStretch);
NebulaSleuth
  • 833
  • 1
  • 7
  • 18