Get selected value from combo box in C# WPF

后端 未结 21 605
粉色の甜心
粉色の甜心 2020-12-08 09:29

I have just started using WPF forms instead of Windows Forms forms. In a Windows Forms form I could just do:

ComboBox.SelectedValue.toString();
相关标签:
21条回答
  • 2020-12-08 10:12

    It works for me:

    System.Data.DataRowView typeItem = (System.Data.DataRowView)ComboBoxName.SelectedItem;
    string value = typeItem.DataView.ToTable("a").Rows[0][0].ToString();
    
    0 讨论(0)
  • 2020-12-08 10:15

    I have figured it out a bit of a strange way of doing it compared to the old WF forms:

    ComboBoxItem typeItem = (ComboBoxItem)cboType.SelectedItem;
    string value = typeItem.Content.ToString();
    
    0 讨论(0)
  • 2020-12-08 10:15

    Create a ComboBox SelectionChanged Event and set ItemsSource="{Binding}" in the WPF design:

    Code:

    private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        string ob = ((DataRowView)comboBox1.SelectedItem).Row.ItemArray[0].ToString();
        MessageBox.Show(ob);
    }
    
    0 讨论(0)
提交回复
热议问题