[英]How to Set a Control's Property Using a String Variable in C# XAML WinRT?
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? 如果可以做到这一点,那么其他类型的控件/属性也可以做到吗?
像这样使用enum.Parse()...
myImage.Stretch = (Stretch)Enum.Parse(typeof(Stretch), imageStretch);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.