Task:
How to determine the selected Wert in WPF at a Combobox
I would like to find out with a Combobox in WPF which number the selected item has.
The items in the Combobox were loaded via ComboBoxItem via content as display text and tag as value
Solution:
You can determine the value or here the Tag/Key via the previous conversion of the ComboBoxItem.
Trick:
By assigning the SelectedValuePath to the day of the SelectedItem, you can save yourself a cumbersome selection.
If you choose SelectedValuePath = Tag, then you can easily assign a numerical value to the complete ComboBox at the start.
<ComboBox x:Name="Combo_IDMaschine" Grid.Row="4" Grid.Column="1" HorizontalAlignment="Left" Width="265" SelectedValuePath="Tag" > </ComboBox>
|
Assigning a number as a value
Combo_IDMaschine.SelectedValue = 123; |
Code Example:
ComboBoxItem item = Combo_IDMaschine.SelectedItem as ComboBoxItem ; data.IDMaschine = (int) item.Tag; //*get value of combobox
//Or data.IDMaschine = (int) ((ComboBoxItem) Combo_IDMaschine.SelectedItem).Tag ; //*get value of combobo
|
Excerpt from surveillance, debug window
Name |
Value |
Type |
|
▶ |
Combo_IDMaschine.SelectedItem |
{System.Windows.Controls.ComboBoxItem: Fräser 1} |
object {System.Windows.Controls.ComboBoxItem} |
▶ |
Combo_IDMaschine |
{System.Windows.Controls.ComboBox Items.Count:6} |
System.Windows.Controls.ComboBox |
Combo_IDMaschine.Tag |
null |
object |
|
Combo_IDMaschine.SelectionBoxItem |
"Fräser 1" |
object {string} |
|
▶ |
Combo_IDMaschine.SelectedValue |
{System.Windows.Controls.ComboBoxItem: Fräser 1} |
object {System.Windows.Controls.ComboBoxItem} |
Combo_IDMaschine.SelectedIndex |
2 |
int |
|
((System.Windows.FrameworkElement)Combo_IDMaschine.SelectedValue).Tag |
4 |
object {int} |
|
((System.Windows.FrameworkElement) Combo_IDMaschine.SelectedItem).Tag |
4 |
object {int} |
Filling a Combobox
With tag as value store for ID or key
private void load_Combobox_Maschinen() { //----------------< load_Combobox_Maschinen() >---------------- Combo_IDMaschine.Items.Clear();
try { //----< Daten laden >---- //< get_Data > var data = (from tbl in db.tblBase_Maschinen orderby tbl.Maschine ascending select tbl); //</ get_Data >
//--< @Loop: Rows >-- foreach (var row in data) { ComboBoxItem item = new ComboBoxItem(); item.Content = row.Maschine; item.Tag= row.IDMaschine; //key-value Combo_IDMaschine.Items.Add(item); } //--</ @Loop: Rows >--
//----</ Daten laden >---- } catch (Exception ex) { MessageBox.Show(ex.Message); } //----------------</ load_Combobox_Maschinen() >---------------- }
|
Reading and storing a Combobox
public void save_Data() { //------------< save_Data() >------------ string sID = tbxID.Text; //< check > if (sID == "") return; //</ check >
int ID = Convert.ToInt32(sID);
tblBase_Mitarbeiter data; data = db.tblBase_Mitarbeiter.SingleOrDefault(n => n.IDMitarbeiter == ID); if (data != null) { data.dtCreated = DateTime.Now; data.Vorname = tbxVorname.Text; data.Nachname = tbxNachname.Text; data.Personalnummer = tbxPersonalNr.Text;
data.IDMaschine = (int) ((ComboBoxItem) Combo_IDMaschine.SelectedItem).Tag ; //*get value of combobox
//< save > db.SaveChanges(); //ID: -99999->16 load_DataGrid(); //</ save > } //------------</ save_Data() >------------ }
|
Assigning a number as a value
Combo_IDMaschine.SelectedValue = 123; |
Example: Loading a record, assigning to a Combobox
public void load_Dataset_byID(int ID) { //----------------< load_Dataset_byID() >---------------- try { var data = db.tblBase_Mitarbeiter.FirstOrDefault(id => id.IDMitarbeiter == ID); if (data != null) { //--< felder fuellen >-- tbxID.Text = data.IDMitarbeiter.ToString(); tbxVorname.Text = data.Vorname.ToString() ; tbxNachname.Text = data.Nachname.ToString(); tbxPersonalNr.Text = data.Personalnummer; Combo_IDMaschine.SelectedValue = data.IDMaschine; //123 //--</ felder fuellen >-- } } catch (Exception ex) { // System.Diagnostics.Debug.WriteLine(ex); }
//----------------</ load_Dataset_byID() >---------------- }
|