App.Config读取按钮枚举值

时间:2013-01-22 11:18:25

标签: c# wpf enums radio-button app-config

我刚刚遇到这个问题,我的课程中有2 RadioButton,每个RadioButton的值应该从class-file-Enum中读取,

让我们说:

Enum Status {    
Good,
Bad
}

我从我的Main调用值没有问题,但我试图把它放到App.Config中这样:

<add key="ClassRadioButton1Status"  value="Good"/>
<add key="ClassRadioButton2Status"  value="Bad"/>

我还为我的app.Config创建了一个通用类:

 public static string ClassRadioButton1Status= ConfigurationManager.AppSettings["ClassRadioButton1Status"];
 public static string ClassRadioButton2Status= ConfigurationManager.AppSettings["ClassRadioButton2Status"];

现在在Main内,我想这样调用RadioButton并从枚举中分配值:

Public void processing (){
    if (RadioButton.Checked == True ){     
    // here i have to Assign the Value from Enum to This:
    Class.eStatus =// how to do from here?!!!!!!    
    }
}

1 个答案:

答案 0 :(得分:0)

这可以通过以下方式实现(如Matt所说):

Public void processing (){
    if (RadioButton.Checked == True ){     
    // here i have to Assign the Value from Enum to This:
    Class.eStatus =(Status) Enum.Parse(typeof(Status), ClassRadioButton1Status);
    }
}

注意

如果有人给AppSettings写了一些垃圾,你也应该检查Enum.IsDefined