2011-07-13 159 views
1

我有一個組合框,它是可編輯的。所以用戶可以選擇一個項目,但是如果項目不存在,他可以輸入他想要的東西。但我的問題是,如果我選擇一個現有的項目,一切正常,並將其值設爲:獲取在可編輯組合框中鍵入的選定值?

<ComboBox Height="23" SelectedIndex="0" HorizontalAlignment="Left" Margin="104,73,0,0" Name="comboBox1" VerticalAlignment="Top" Width="159" IsEditable="True" SelectionChanged="comboBox1_SelectionChanged" /> 

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      ToetsAlgemeneGegevensViewModel vm = (ToetsAlgemeneGegevensViewModel)this.DataContext; 
      if (comboBox1.SelectedValue != null && vm != null) 
      { 


       vm.Examination.Course = comboBox1.SelectedValue.ToString(); 
      } 

但是,如果我在輸入一些東西,我怎麼可以設置這個值?有人知道如何做到這一點?

回答

2

快速回答:

我想你應該最好使用ComboBox.Text屬性。在視圖模型中創建一個字符串屬性並將其綁定到Text屬性中:Text="{Binding MyStringProperty}"

做你在你的字符串屬性setter中的comboBox1_SelectionChanged做什麼。我認爲這將是足夠的。