开发者

Getting a selected value which is typed in an editable combobox?

I have a combobox and it's editable. So the user can select an item, but if the item doesn't exist, he can type in what ever he wants. But my problem is , if i select an existing item, everything works, and the value is set :

  <ComboBox  Height="23"  SelectedIndex="0"  HorizontalAlignment="Left" Margin="104,73,0,0" Name="comboBox1" VerticalAlignment="Top" Width="159" IsEditable="True" SelectionChanged="comboBo开发者_运维技巧x1_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();
            }

But, if I type in something, how can i set this value? Someone who knows how to do this?


A quick answer:

I think you should better use ComboBox.Text property. Make a string property in your view model and bind it in the Text property: Text="{Binding MyStringProperty}".

Do what you do in your comboBox1_SelectionChanged inside the setter of your string property. I think this will be enough.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜