2010-05-18 110 views
1

我怎樣才能在下拉框中的SelectionChanged事件選定的文本 這裏是我的代碼選擇的組合框的文本

<ComboBox x:Name="cboRecordType" Margin="2,0" Height="23" Grid.Column="1" VerticalAlignment="Center" SelectionChanged="ComboBox_SelectionChanged"> 
      <ComboBoxItem Content="Weight"/> 
      <ComboBoxItem Content="Height"/> 
      <ComboBoxItem Content="Blood Pressure"/> 
      <ComboBoxItem Content="Blood Gulocose"/> 
     </ComboBox> 

cboRecordType.Text是空的,沒有cantain選定的文本,如何拿到

回答

1

SelectionChanged事件處理程序中,您既可以查看組合框本身的cboRecordType.SelectedItem屬性,也可以查看傳遞到事件處理程序的SelectionChangedEventArgsAddedItems屬性。

選擇某個項目時,該項目將被添加到事件參數的AddedItems數組屬性中。 (多選案例中的多個項目)。當一個項目被取消選擇時,它被添加到事件參數的RemovedItems數組屬性中。

+0

它會返回以下值 {System.Windows.Controls.ComboBoxItem:Weight} 但我只需要重量我怎麼能得到它 – 2010-05-18 06:50:30

+0

@Asim - 使用SelectedItem.Content? – Gishu 2010-05-18 07:50:02

+0

@Gishu:沒有看到SelectedItem的內容的任何屬性 – 2010-05-18 09:10:05

0

在後面的代碼,你需要處理這樣的代碼事件:ComboBox SelectionChanged 代碼塊

/// <summary> 

/// Handles the comboBox SelectionChanged event 

/// </summary> 

/// <param name="sender"></param> 

/// <param name="e"></param> 

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 

{ 



} 
0

您可以嘗試綁定方法,而不是處理事件。對於您需要創建一個屬性喜歡這一點,並把它綁定到你的組合框的選擇項

private String _selectedItem; 
public String SelectedItem 
{ 
    get { return _selectedItem; } 
    set 
    { 
     _selectedItem = value; 
     OnPropertyChanged(new PropertyChangedEventArgs("SelectedItem")); 
    } 
} 

<ComboBox SelectedItem="{Binding SelectedItem}" /> 

旁註:您還可以填寫一些收集,並將其綁定到組合框,而不是硬編碼

0

更好的請嘗試使用Command和CommandParametar作爲MVVM實現的一部分。