I have created a combo box with data binding in WPF. I am not sure how to get value and set value of “comboboxselecteditem”

前端 未结 2 1526
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-24 05:49

This my Xaml code for combo box with data binding :

        

        
2条回答
  •  北恋
    北恋 (楼主)
    2021-01-24 06:07

    You could cast the SelectedItem property of the ComboBox to a CUSTOMER object to get the currently selected item:

    CUSTOMER selectedCustomer = C1.SelectedItem as CUSTOMER;
    if(selectedCustomer != null)
        MessageBox.Show(selectedCustomer.CUSTOMER_NAME);
    

    To select an item you set the SelectedItem property to a CUSTOMER object that is included in the ComboBox's ItemsSource:

    C1.SelectedItem = C1.Items[0]; //selects the first customer (index = 0)
    

    Or if you know the name or description of the customer:

    string theName = "some customer name...";
    C1.SelectedItem = C1.Items.OfType().FirstOrDefault(x => x.CUSTOMER_NAME == theName);
    

提交回复
热议问题