简体   繁体   English

如何在C#XAML WinRT中使用字符串变量设置控件的属性?

[英]How to Set a Control's Property Using a String Variable in C# XAML WinRT?

How can I set the property for a XAML control using a string variable? 如何使用字符串变量设置XAML控件的属性?

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.

相关问题 如何在UWP中使用C#中的字符串名称更改xaml按钮属性? - How to change a xaml button property using it's string name from c# in UWP? 如何在WinRT XAML C#中克隆UIElement? - How to clone UIElement in WinRT XAML C#? 如何在C#中将变量绑定到控件的text属性? - How to bind a variable to a control's text property in C#? 如何在XAML / C#上虚拟化“像数据网格一样”水平和垂直控制(Windows 8.1 - WinRT) - How can I virtualize a “datagrid like” Control Horizontally and Vertically on XAML/C# (Windows 8.1 - WinRT) 在XAML中使用来自自定义控件的C#变量(Silverlight) - Using a C# variable from a custom control in xaml (silverlight) 如何序列化和反序列化字典 <string,object> 在WinRT中(C#和XAML中的Metro应用)? - How to serialize and deserliaze a Dictionary<string,object> in WinRT (Metro app in C# and XAML)? Winrt Xaml工具包绑定到控件的子属性 - Winrt Xaml Toolkit bind to sub property of control 在WinRT C#中,如何使用RenderTargetBitmap保存屏幕外XAML树? - In WinRT C# how do I save an offscreen XAML tree using RenderTargetBitmap? C#如何修改控件的属性 - c# how to modify the control's property WinRT ListView,将itemsource作为列表 <KeyValuePair<string, string> &gt;错误XAML(Windows 8.1通用应用程序(XAML / C#)) - WinRT ListView with itemsource as List<KeyValuePair<string, string>> error XAML (Windows 8.1 Universal App (XAML/C#))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM