简体   繁体   English

使用C#填充ComboBox

[英]Populating a ComboBox using C#

I would like to populate a combobox with the following: 我想用以下内容填充一个组合框:

Visible item / Item Value 可见项目/项目值

English / En

Italian / It

Spainish / Sp 

etc....

Any help please? 有什么帮助吗?

Also it is possible that after populating the Combobox, to make it read only? 也可以在填充Combobox后,使其只读?

Define a class 定义一个类

public class Language
{
     public string Name { get; set; }
     public string Value { get; set; }
}

then... 然后...

//Build a list
var dataSource = new List<Language>();
dataSource.Add(new Language() { Name = "blah", Value = "blah" });
dataSource.Add(new Language() { Name = "blah", Value = "blah" });
dataSource.Add(new Language() { Name = "blah", Value = "blah" });

//Setup data binding
this.comboBox1.DataSource = dataSource;
this.comboBox1.DisplayMember = "Name";
this.comboBox1.ValueMember = "Value";

// make it readonly
this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

Create a class Language 创建一个类语言

public class Language
{
     public string Name{get;set;}
     public string Value{get;set;}
     public override string ToString() { return this.Name;}
}

Then, add as many language to the combobox that you want: 然后,为所需的组合框添加尽可能多的语言:

yourCombobox.Items.Add(new Language{Name="English",Value="En"});

Set the ValueMember / DisplayMember properties to the name of the properties of your Language objects. ValueMember / DisplayMember属性设置为Language对象的属性名称。

class Language
{
    string text;
    string value;

    public string Text
    {
        get 
        {
            return text;
        }
    }

    public string Value
    {
        get
        {
            return value;
        }
    }

    public Language(string text, string value)
    {
        this.text = text;
        this.value = value;
    }
}

...

combo.DisplayMember= "Text";
combo.ValueMember = "Value";
combo.Items.Add(new Language("English", "en"));

To make it read-only, the DropDownStyle property to DropDownStyle.DropDownList. 要使其为只读,DropDownStyle属性为DropDownStyle.DropDownList。

To populate the ComboBox, you will need to have a object like Language or so containing both for instance: 要填充ComboBox,您需要有一个像Language这样的对象,例如:

public class Language {
    public string Name { get; set; }
    public string Code { get; set; }
}

Then, you may bind a IList to your ComboBox.DataSource property like so: 然后,您可以将IList绑定到ComboBox.DataSource属性,如下所示:

IList<Language> languages = new List<Language>();
languages.Add(new Language("English", "en"));
languages.Add(new Language("French", "fr"));

ComboxBox.DataSource = languages;
ComboBox.DisplayMember = "Name";
ComboBox.ValueMember = "Code";

This will do exactly what you expect. 这将完全符合您的期望。

  Language[] items = new Language[]{new Language("English", "En"),
                new Language("Italian", "It")};

            languagesCombo.ValueMember = "Alias";
            languagesCombo.DisplayMember = "FullName";
            languagesCombo.DataSource = items.ToList();

            languagesCombo.DropDownStyle = ComboBoxStyle.DropDownList;

 class Language
    {
        public string FullName { get; set; }
        public string Alias { get; set; }

        public Language(string fullName, string alias)
        {
            this.FullName = fullName;
            this.Alias = alias;
        }
    }

By making your drop down box "read-only" I am assuming you want to prevent user's typing in other options as opposed to being fully read-only where users cannot select a value?? 通过使您的下拉框“只读”我假设您想要阻止用户键入其他选项而不是完全只读用户无法选择值?

If you wanted it to be fully read-only you could set the enabled property to be false. 如果您希望它完全只读,则可以将enabled属性设置为false。

What you could do is create a new class, similar to @Gregoire's example, however, you would want to override the ToString() method so it appears correctly in the combo box eg 您可以做的是创建一个新类,类似于@ Gregoire的示例,但是,您可能希望覆盖ToString()方法,以便它在组合框中正确显示,例如

public class Language
{
    private string _name;
    private string _code;

    public Language(string name, string code)
    {
        _name = name;
        _code = code;
    }

    public string Name { get { return _name; }  }
    public string Code { get { return _code; } }
    public override void ToString()
    {
        return _name;
    }
}

Simple way is: 简单的方法是:

Dictionary<string, string> dict = new Dictionary<string, string>()
{
    {"English ","En" },
    {"Italian  ","It" },
    {"Spainish  ","Sp " }
};

combo.DataSource = new BindingSource(dict, null);
combo.DisplayMember = "Key";
combo.ValueMember = "Value";

No need to use a particular class Language, 无需使用特定的语言,

Just replace it with : 只需将其替换为:

KeyValuePair<string,string>

If you simply want to add it without creating a new class try this: 如果您只是想在不创建新类的情况下添加它,请尝试以下方法:

// WPF
<ComboBox Name="language" Loaded="language_Loaded" /> 


// C# code
private void language_Loaded(object sender, RoutedEventArgs e)
{
    List<String> language= new List<string>();
    language.Add("English");
    language.Add("Spanish");
    language.Add("ect"); 
    this.chartReviewComboxBox.ItemsSource = language;
}

I suggest an xml file with all your languages that you will support that way you do not have to be dependent on c# I would definitly create a class for languge like the above programmer suggest. 我建议用你所支持的所有语言的xml文件,你不必依赖c#我会像上面的程序员建议的那样创建一个语言类。

but do you not just get your combo box name and then items.add("") ? 但是你不只是得到你的组合框名称然后items.add("")

For instance 例如

Language.Items.Add("Italian");
Language.Items.Add("English");
Language.Items.Add("Spanish");

Hope this helped :D 希望这有助于:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM