I would like to populate a combobox with the following:
Visible item / Item Value
English / En
Italian / It
Spainish / Sp
etc...
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.
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.
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 e.g.
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;
}
}
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;