获取在可编辑组合框中输入的选定值?

时间:2011-07-13 12:40:46

标签: wpf combobox

我有一个组合框,它是可编辑的。因此用户可以选择一个项目,但如果该项目不存在,他可以输入他想要的内容。但我的问题是,如果我选择一个现有项目,一切正常,并设置值:

  <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();
            }

但是,如果我输入某些内容,我该如何设置此值?谁知道怎么做?

1 个答案:

答案 0 :(得分:2)

快速回答:

我认为你应该更好地使用ComboBox.Text属性。在视图模型中创建一个字符串属性,并将其绑定在Text属性中:Text="{Binding MyStringProperty}"

在你的字符串属性的setter中的comboBox1_SelectionChanged中执行的操作。我认为这就足够了。