我想通过使用所有国家名称列表填充ComboBox

Using System.Globalization
Using System.Collections


这是我对ComboBox的XAML编码

<ComboBox x:Name="countryComboBox" Width="300"/>


在C#编码中,我具有添加按钮。这是代码,我想从ComboBox获取国家/地区并将其存储在MySql数据库中。我怎样才能做到这一点?

private void addButton_Click(object sender, RoutedEventArgs e)
{
    try
    {
        string Query = @"INSERT INTO `bcasdb`.`tbl_student`
                         (`reg_id`,
                          `std_fname`,
                          `std_lname`,
                          `tbl_batch_batch_id`,
                          `gender`)
                         VALUES (@regId, @fName, @lName, @bID, @gender, @country)";
        //This is command class which will handle the query and connection object.
        MySqlConnection conn = new MySqlConnection(BCASApp.DataModel.DB_CON.connection);
        MySqlCommand cmd = new MySqlCommand(Query, conn);
        conn.Open();
        cmd.Parameters.AddWithValue("@regId", this.regIDInput.Text);
        cmd.Parameters.AddWithValue("@fName", this.fnameInput.Text);
        cmd.Parameters.AddWithValue("@lName", this.lnameInput.Text);
        cmd.Parameters.AddWithValue("@bID", this.batchIDInput.Text);
        cmd.Parameters.AddWithValue("@gender",this.maleRadioButton);
        cmd.ExecuteNonQuery();
        conn.Close();
        successmsgBox();
    }
    catch (Exception)
    {
        errormsgBox();
    }
}

最佳答案

获取所有国家/地区的代码:

using System.Globalization;
using System.Runtime.InteropServices;

public class LocalesRetrievalException : Exception
{
    public LocalesRetrievalException(string message)
        : base(message)
    {
    }
}

#region Windows API

private delegate bool EnumLocalesProcExDelegate(
   [MarshalAs(UnmanagedType.LPWStr)]String lpLocaleString,
   LocaleType dwFlags, int lParam);

[DllImport(@"kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool EnumSystemLocalesEx(EnumLocalesProcExDelegate pEnumProcEx,
   LocaleType dwFlags, int lParam, IntPtr lpReserved);

private enum LocaleType : uint
{
    LocaleAll = 0x00000000,             // Enumerate all named based locales
    LocaleWindows = 0x00000001,         // Shipped locales and/or replacements for them
    LocaleSupplemental = 0x00000002,    // Supplemental locales only
    LocaleAlternateSorts = 0x00000004,  // Alternate sort locales
    LocaleNeutralData = 0x00000010,     // Locales that are "neutral" (language only, region data is default)
    LocaleSpecificData = 0x00000020,    // Locales that contain language and region data
}

#endregion

public enum CultureTypes : uint
{
    SpecificCultures = LocaleType.LocaleSpecificData,
    NeutralCultures = LocaleType.LocaleNeutralData,
    AllCultures = LocaleType.LocaleWindows
}

public static List<CultureInfo> GetCultures(CultureTypes cultureTypes)
{
    List<CultureInfo> cultures = new List<CultureInfo>();
    EnumLocalesProcExDelegate enumCallback = (locale, flags, lParam) =>
    {
        try
        {
            cultures.Add(new CultureInfo(locale));
        }
        catch (CultureNotFoundException)
        {
            // This culture is not supported by .NET (not happened so far)
            // Must be ignored.
        }
        return true;
    };

    if (EnumSystemLocalesEx(enumCallback, (LocaleType)cultureTypes, 0, (IntPtr)0) == false)
    {
        int errorCode = Marshal.GetLastWin32Error();
        throw new LocalesRetrievalException("Win32 error " + errorCode + " while trying to get the Windows locales");
    }

    // Add the two neutral cultures that Windows misses
    // (CultureInfo.GetCultures adds them also):
    if (cultureTypes == CultureTypes.NeutralCultures || cultureTypes == CultureTypes.AllCultures)
    {
        cultures.Add(new CultureInfo("zh-CHS"));
        cultures.Add(new CultureInfo("zh-CHT"));
    }

    return cultures;
}

public static List<string> GetCountries()
{
    List<CultureInfo> cultures = GetCultures(CultureTypes.SpecificCultures);
    List<string> countries = new List<string>();

    foreach (CultureInfo culture in cultures)
    {
        RegionInfo region = new RegionInfo(culture.Name);

        if (!(countries.Contains(region.EnglishName)))
        {
            countries.Add(region.EnglishName);
        }
    }

    return countries;
}


如何使用此代码:

您只需要调用方法GetCountries

comboBoxCountries.ItemsSource = GetCountries();


参考:Replacement for CultureInfo.GetCultures in .NET Windows Store apps

关于c# - 获取comboBox中的所有国家,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26311821/

10-13 08:05