如何绑定wpf ComboBox来更改语言而不写入xaml.cs?

时间:2011-06-09 03:54:57

标签: c# wpf wpf-controls binding

我有如下wpf组合框。

<ComboBox  Margin="71.421,3.417,-0.002,2.998" Width="167.244" Height="20" ToolTip="Change language settings" Padding="4,1,4,0" VerticalContentAlignment="Center" >
                    <ComboBoxItem Content="English" IsSelected="True"   />
                    <ComboBoxItem Content="中文" />
</ComboBox>

而且,我在下面有这个文化课。

public class SpecificCultures
    {
        public static readonly string AR_SA = "ar-SA";
        public static readonly string BG_BG = "bg-BG";
        public static readonly string CA_ES = "ca-ES";
        public static readonly string ZH_TW = "zh-TW";
        public static readonly string CS_CZ = "cs-CZ";
        public static readonly string DA_DK = "da-DK";
        public static readonly string DE_DE = "de-DE";
        public static readonly string EL_GR = "el-GR";
        public static readonly string EN_US = "en-US";
        public static readonly string FI_FI = "fi-FI";
        public static readonly string FR_FR = "fr-FR";
        public static readonly string HE_IL = "he-IL";
        public static readonly string HU_HU = "hu-HU";
        public static readonly string IS_IS = "is-IS";
        public static readonly string IT_IT = "it-IT";
        public static readonly string JA_JP = "ja-JP";
        public static readonly string KO_KR = "ko-KR";
        public static readonly string NL_NL = "nl-NL";
        public static readonly string NB_NO = "nb-NO";
        public static readonly string PL_PL = "pl-PL";
        public static readonly string PT_BR = "pt-BR";
        public static readonly string RO_RO = "ro-RO";
        public static readonly string RU_RU = "ru-RU";
        public static readonly string HR_HR = "hr-HR";
        public static readonly string SK_SK = "sk-SK";
        public static readonly string SQ_AL = "sq-AL";
        public static readonly string SV_SE = "sv-SE";
        public static readonly string TH_TH = "th-TH";
        public static readonly string TR_TR = "tr-TR";
        public static readonly string UR_PK = "ur-PK";
        public static readonly string ID_ID = "id-ID";
        public static readonly string UK_UA = "uk-UA";
        public static readonly string BE_BY = "be-BY";
        public static readonly string SL_SI = "sl-SI";
        public static readonly string ET_EE = "et-EE";
        public static readonly string LV_LV = "lv-LV";
        public static readonly string LT_LT = "lt-LT";
        public static readonly string FA_IR = "fa-IR";
        public static readonly string VI_VN = "vi-VN";
        public static readonly string HY_AM = "hy-AM";
        public static readonly string AZ_LATN_AZ = "az-Latn-AZ";
        public static readonly string EU_ES = "eu-ES";
        public static readonly string MK_MK = "mk-MK";
        public static readonly string AF_ZA = "af-ZA";
        public static readonly string KA_GE = "ka-GE";
        public static readonly string FO_FO = "fo-FO";
        public static readonly string HI_IN = "hi-IN";
        public static readonly string MS_MY = "ms-MY";
        public static readonly string KK_KZ = "kk-KZ";
        public static readonly string KY_KG = "ky-KG";
        public static readonly string SW_KE = "sw-KE";
        public static readonly string UZ_LATN_UZ = "uz-Latn-UZ";
        public static readonly string TT_RU = "tt-RU";
        public static readonly string PA_IN = "pa-IN";
        public static readonly string GU_IN = "gu-IN";
        public static readonly string TA_IN = "ta-IN";
        public static readonly string TE_IN = "te-IN";
        public static readonly string KN_IN = "kn-IN";
        public static readonly string MR_IN = "mr-IN";
        public static readonly string SA_IN = "sa-IN";
        public static readonly string MN_MN = "mn-MN";
        public static readonly string GL_ES = "gl-ES";
        public static readonly string KOK_IN = "kok-IN";
        public static readonly string SYR_SY = "syr-SY";
        public static readonly string DV_MV = "dv-MV";
        public static readonly string AR_IQ = "ar-IQ";
        public static readonly string ZH_CN = "zh-CN";
        public static readonly string DE_CH = "de-CH";
        public static readonly string EN_GB = "en-GB";
        public static readonly string ES_MX = "es-MX";
        public static readonly string FR_BE = "fr-BE";
        public static readonly string IT_CH = "it-CH";
        public static readonly string NL_BE = "nl-BE";
        public static readonly string NN_NO = "nn-NO";
        public static readonly string PT_PT = "pt-PT";
        public static readonly string SR_LATN_CS = "sr-Latn-CS";
        public static readonly string SV_FI = "sv-FI";
        public static readonly string AZ_CYRL_AZ = "az-Cyrl-AZ";
        public static readonly string MS_BN = "ms-BN";
        public static readonly string UZ_CYRL_UZ = "uz-Cyrl-UZ";
        public static readonly string AR_EG = "ar-EG";
        public static readonly string ZH_HK = "zh-HK";
        public static readonly string DE_AT = "de-AT";
        public static readonly string EN_AU = "en-AU";
        public static readonly string ES_ES = "es-ES";
        public static readonly string FR_CA = "fr-CA";
        public static readonly string SR_CYRL_CS = "sr-Cyrl-CS";
        public static readonly string AR_LY = "ar-LY";
        public static readonly string ZH_SG = "zh-SG";
        public static readonly string DE_LU = "de-LU";
        public static readonly string EN_CA = "en-CA";
        public static readonly string ES_GT = "es-GT";
        public static readonly string FR_CH = "fr-CH";
        public static readonly string AR_DZ = "ar-DZ";
        public static readonly string ZH_MO = "zh-MO";
        public static readonly string DE_LI = "de-LI";
        public static readonly string EN_NZ = "en-NZ";
        public static readonly string ES_CR = "es-CR";
        public static readonly string FR_LU = "fr-LU";
        public static readonly string AR_MA = "ar-MA";
        public static readonly string EN_IE = "en-IE";
        public static readonly string ES_PA = "es-PA";
        public static readonly string FR_MC = "fr-MC";
        public static readonly string AR_TN = "ar-TN";
        public static readonly string EN_ZA = "en-ZA";
        public static readonly string ES_DO = "es-DO";
        public static readonly string AR_OM = "ar-OM";
        public static readonly string EN_JM = "en-JM";
        public static readonly string ES_VE = "es-VE";
        public static readonly string AR_YE = "ar-YE";
        public static readonly string EN_029 = "en-029";
        public static readonly string ES_CO = "es-CO";
        public static readonly string AR_SY = "ar-SY";
        public static readonly string EN_BZ = "en-BZ";
        public static readonly string ES_PE = "es-PE";
        public static readonly string AR_JO = "ar-JO";
        public static readonly string EN_TT = "en-TT";
        public static readonly string ES_AR = "es-AR";
        public static readonly string AR_LB = "ar-LB";
        public static readonly string EN_ZW = "en-ZW";
        public static readonly string ES_EC = "es-EC";
        public static readonly string AR_KW = "ar-KW";
        public static readonly string EN_PH = "en-PH";
        public static readonly string ES_CL = "es-CL";
        public static readonly string AR_AE = "ar-AE";
        public static readonly string ES_UY = "es-UY";
        public static readonly string AR_BH = "ar-BH";
        public static readonly string ES_PY = "es-PY";
        public static readonly string AR_QA = "ar-QA";
        public static readonly string ES_BO = "es-BO";
        public static readonly string ES_SV = "es-SV";
        public static readonly string ES_HN = "es-HN";
        public static readonly string ES_NI = "es-NI";
        public static readonly string ES_PR = "es-PR";
        public static readonly string SMA_NO = "sma-NO";
        public static readonly string SR_CYRL_BA = "sr-Cyrl-BA";
        public static readonly string ZU_ZA = "zu-ZA";
        public static readonly string XH_ZA = "xh-ZA";
        public static readonly string FY_NL = "fy-NL";
        public static readonly string TN_ZA = "tn-ZA";
        public static readonly string SE_SE = "se-SE";
        public static readonly string SMA_SE = "sma-SE";
        public static readonly string HR_BA = "hr-BA";
        public static readonly string SMN_FI = "smn-FI";
        public static readonly string QUZ_PE = "quz-PE";
        public static readonly string SE_FI = "se-FI";
        public static readonly string SMS_FI = "sms-FI";
        public static readonly string CY_GB = "cy-GB";
        public static readonly string BS_LATN_BA = "bs-Latn-BA";
        public static readonly string BS_CYRL_BA = "bs-Cyrl-BA";
        public static readonly string FIL_PH = "fil-PH";
        public static readonly string SMJ_NO = "smj-NO";
        public static readonly string ARN_CL = "arn-CL";
        public static readonly string IU_LATN_CA = "iu-Latn-CA";
        public static readonly string MI_NZ = "mi-NZ";
        public static readonly string QUZ_EC = "quz-EC";
        public static readonly string GA_IE = "ga-IE";
        public static readonly string SR_LATN_BA = "sr-Latn-BA";
        public static readonly string MOH_CA = "moh-CA";
        public static readonly string SMJ_SE = "smj-SE";
        public static readonly string LB_LU = "lb-LU";
        public static readonly string NS_ZA = "ns-ZA";
        public static readonly string QUZ_BO = "quz-BO";
        public static readonly string SE_NO = "se-NO";
        public static readonly string MT_MT = "mt-MT";
        public static readonly string RM_CH = "rm-CH";

    }

在ViewModel.cs中,

如果改变了梳理箱选择,

我需要调用函数来保存用户选择的文化。

例如

 SaveCuluterID(string culutredID)
{
   // process here to change language of the application, this is already done and working. 
}

如果用户选择英语,cultureID应为SpecificCultures.EN_US。如果它是中国人,应该是 SpecificCultures.ZH_CN。如果我写入xaml.cs它会工作。但是,我需要在ViewModel.cs中执行此逻辑,因为我们的工作流需要全部关于绑定。无论用户选择什么,我都需要将其更改为CultureID。感谢。

2 个答案:

答案 0 :(得分:1)

我建议将每个ComboBoxItem的“Tag”属性设置为您想要的文化或字符串,然后将SelectedValuePath设置为指向“Tag”,以便它将绑定到您想要的属性。

这是一个很好的例子/解释: http://johnpapa.net/binding-to-silverlight-combobox-and-using-selectedvalue-selectedvaluepath-and-displaymemberpath

答案 1 :(得分:0)

您可以将Selected item绑定到viewmodel

中声明的属性
SelectedItem={Binding SelectedCulture}