2016-11-24 111 views
0

我們可以在沒有數據綁定的情況下從組合框中檢索內容嗎?這是我的XAML代碼從C#組合框獲取內容值

<ComboBox x:Name="Choice" Header="Choice your eating time" PlaceholderText="Pilih" Width="200" SelectionChanged="Choice_SelectionChanged"> 
      <ComboBoxItem Content="Breakfast" /> 
      <ComboBoxItem Content="Lunch" /> 
      <ComboBoxItem Content="Dinner"/> 
</ComboBox> 

我試着用choice.SelectedItem.ToString()但結果將是Windows.UI.XAML.ComboBox,組合框的不是內容。我想將它傳遞到列表框中,當我使用choice.SelectedValue.ToString()時,它包含與SelectedItem相同的結果。

+0

嘗試'SelectedValue'屬性 – NtFreX

+2

可能的重複http://stackoverflow.com/questions/4902039/difference-between-selecteditem-selectedvalue-and-selectedvaluepath –

+0

@ThomasWeller,我明白SelectedItems,SelectedIndex和SelectedValue之間的區別,我試圖將它傳遞給列表框,它顯示Windows.UI.Xaml.ComboBox(我忘了重新SULT)。 –

回答

1

@Olivia奧爾加克拉麗莎試試這個..

Text = ((ComboBoxItem)Choice.SelectedItem).Content.ToString(); 

or 

var comboBoxItem = Choice.Items[Choice.SelectedIndex] as ComboBoxItem; 
if (comboBoxItem != null) 
{ 
    string selectedcmb = comboBoxItem.Content.ToString(); 
} 
+0

謝謝你的幫助!我嘗試了第一個,現在它正在工作。我用於傳遞給列表框。 –

0

使用choice.SelectedItem.Content,如果你看不到屬性,將其轉換爲((ContentControl中)choice.SelectedItem)。內容