C# 获取字符串变量的值

C# 获取字符串变量的值,c#,wpf,C#,Wpf,如何用第二行中的字符串变量样式替换字符串文字-“ErrorLabel”?仅供参考。样式名称将存储在xml文件中 string style = "ErrorLabel"; Style styItem = LayoutRoot.Resources["ErrorLabel"] as Style; fld.Settings.CellValuePresenterStyle = styItem; 这应该做到: string style = "ErrorLabel"; Style styItem =

如何用第二行中的字符串变量样式替换字符串文字-“ErrorLabel”?仅供参考。样式名称将存储在xml文件中

 string style = "ErrorLabel";
 Style styItem = LayoutRoot.Resources["ErrorLabel"] as Style;
 fld.Settings.CellValuePresenterStyle = styItem;
这应该做到:

string style = "ErrorLabel";
Style styItem = LayoutRoot.Resources[style] as Style;
fld.Settings.CellValuePresenterStyle = styItem;

谢谢现在就行了,我觉得自己很蠢!出于某种原因,之前编译器一直说我不能使用变量,即使ResourceDictionary的键是object类型!