The following applies to add rows to the controls in ComboBox C# WPF. Presented examples refer to controls named “comboPerson“.
Contents
Add a record to the ComboBox C# WPF
Add another row in the ComboBox is implemented by using the function Items.Add(); As an argument, you must enter a new object (Item), which can be simply the String.
And so the command comboPerson.Items.Add(„String”); will add a new row with a value of “Words” to have been named “comboPerson”. So a complement to the list you can realize through the command:
comboPerson.Items.Add(„Napis1”);
comboPerson.Items.Add(„Napis2”);
comboPerson.Items.Add(„Napis3”);
Add records in the loop
Of course, manually typing each item would be time consuming and cumbersome, so you should use for this task.
Before proceeding to supplement the data, you can clear the existing entries, this is relevant when the data are read for example. from the database and may have changed since the previous load operation – this is a feature Items.Clear().
After ComboBox the data we would like to have appeared in the running program. If the list has changed, you must refresh the function Refresh(), otherwise you will not see the changes.
comboPerson.Items.Clear(); //Opróżnij comboPerson
while (readPersons.Read())
{
comboPerson.Items.Add(readPersons.GetString(1));
}
comboPerson.Items.Refresh();//Odśwież kontrolkę w oknie programu
In the above loop reader used information from the database, named “readPersons,” which in each iteration of the loop returns a string with the current No. 1 column of the table. Presented with a piece of code to complement the ComboBox’a all the values in the columns of the selected table.
Add records with two columns
And what if in addition to values, we also indexes (or primary key) selected table? Then it should be completed this information and display only those items that you should see.
For that purpose you can use a class Dictionary, it is a collection class that allows you to add items to the list, along with their keys. It is in the System.Collections.Generic namespace.
In the Declaration, you must specify the type of the input, for example Dictionary<int, string>, which means that the first element in each row will be of type int, and the second type string.</int,> Both elements are stored as two columns named “Key” and “Value” and thus can be referenced.
The difference in the code is that the loop does not fill up with new ComboBox records, but just a collection of Dictionary and finally we ascribe it to the ComboBox ItemsSource. Function Clear() in this case will result in an error, so you must first delete the values from the ItemsSource by comparison to null.
The following code shows the development of the previous example.
comboPerson.ItemsSource = null;
this.comboPerson.Items.Clear();//Nie wolno używać gdy jest podpięty ItemsSource, chyba że jest null'em
System.Collections.Generic.Dictionary<int, string> comboSource = new System.Collections.Generic.Dictionary<int, string>();
while (readPersons.Read())
{
comboSource.Add(readPersons.GetInt32(2), readPersons.GetString(1));
}
comboPerson.ItemsSource = comboSource;//Przypisanie kolekcji do ComboBox'a
comboPerson.DisplayMemberPath = "Value";//Wybór kolumny do wyświetlenia
this.comboPerson.Items.Refresh();
Wybór kolumny, która zostanie wyświetlona na liście odbywa się za pomocą komendy comboPerson.DisplayMemberPath = „Value”; Ponieważ do ComboBox’a została przypisana kolejka Dictionary to do wyboru są tu tylko dwie kolumny o przedstawionych wcześniej nazwach „Key” oraz „Value”.
A selection of columns to be displayed in the list is done using the command comboPerson.DisplayMemberPath = „Value”; Since the Dictionary queue has been assigned to the ComboBox, there are only two columns here with the names “Key” and “Value” presented earlier.
Reading selected values in the ComboBox
When the controls have been supplemented with data, it remains only to read the values chosen by users. The examples will be projected onto a string so that they can be displayed eg in the MessageBox.
Read the full line:
comboPerson.SelectedValue.ToString();
[2, Jan]
Displays the number representing the index and the string value in square brackets.
To display the value of the Key or Value column, the selected ComboBox element (SelectedItem) should be projected onto the KeyValuePair located in the System.Collections.Generic namespace, which will allow access to the field:
– Key
((System.Collections.Generic.KeyValuePair<int, string>)comboPerson.SelectedItem).Key.ToString();
– Value
((System.Collections.Generic.KeyValuePair<int, string>)comboPerson.SelectedItem).Value.ToString();