1

I need the ComboBox below to keep the SelectedItem when I edit the text, at the moment as soon as I edit the text the SelectedItem turns null.

        <ComboBox x:Name="FilterGroups"
                  IsEditable="True"
                  DisplayMemberPath="Code"
                  Text="{Binding FilterGroupCode}"
                  ItemsSource="{Binding FilterGroups}"
                  SelectedItem="{Binding SelectedFilterGroup}"
                  Margin="10,0"/>

My goal is to detect that the item has been edited, currently I have no idea which item was edited since the SelectedItem becomes null.

And a null SelectedItem to me at the moment means "New" item.

sprocket12
  • 5,368
  • 18
  • 64
  • 133
  • Have you tried setting the UpdateSourceTrigger property? – AGH Dec 07 '15 at 10:02
  • @AGH no I didn't yet. Could you expand on how that could be used to achieve my goal? – sprocket12 Dec 07 '15 at 10:09
  • This is just an assumption, but as soon as you edit the text of the combobox, the SelectedItem becomes null because it is not a value found in FilterGroups. Setting UpdateSourceTrigger=LostFocus will update SelectedItem only after you have made a selection in the combobox and thus will not be null. This question might also be of help: http://stackoverflow.com/questions/4770912/how-to-use-update-source-trigger-on-wpf-combobox-which-is-editable – AGH Dec 07 '15 at 10:16

1 Answers1

0

As AGH's Comment.

Xaml

    <ComboBox x:Name="myFilterGroups" 
              ItemsSource="{Binding FilterGroups}" 
              DisplayMemberPath="Name" 
              SelectedItem="{Binding SelectedFilterGroup}" 
              IsEditable="True" Height="20"
              LostFocus="myFilterGroups_LostFocus"
              >
    </ComboBox>

Code Behind

    private void myFilterGroups_LostFocus(object sender, RoutedEventArgs e)
    {
        var selItem = myFilterGroups.SelectedItem;

        if (selItem == null)
            // Create New Item
            ;
    }
serendip
  • 58
  • 8