Silverlight - how do I get the text of the selected item in a combobox

后端 未结 7 1164
臣服心动
臣服心动 2021-01-18 13:14

Easy one for you all...

I\'m new to Silverlight and really missing stuff like DataTables and things. What I\'m also currently struggling with is how to get the text

7条回答
  •  再見小時候
    2021-01-18 13:16

    The selected item of your combo box is whatever type of item is currently holding. So if you set the binding to a collection of strings, then the selected item will be a string:

    string mySelectedValue = ((string)MyComboBox.SelectedItem);
    

    If it is a more complex object you will need to cast and use the expected object. If you have XAML using the list box item class, like:

    
        
            
                
            
        
    
    

    Then you would access the selected item like this:

    string mySelectedValue = 
      ((TextBlock)((ComboBoxItem)MyComboBox.SelectedItem).Content).Text;
    

提交回复
热议问题