none
Clearing a ComboBox Selection - MVVM RRS feed

  • Question

  • I have built a WPF app using full MVVM databinding and am a little stuck at how I am suppose to clear a ComboBox's display value. (I select a value from the list " KeyValuePair<double, string>" and I want to reset the box to a blank value. I want to leave the collection in place, just reset the ComboBox Selection.)

     

    I have played around w/ binding to Text and SelectedItem just not sure if I am doing the right syntax or the right properties. I setup a public KeyValuePair<double, string> 'Combo' w/ a getter and a setter and NotifyProperty Change. It is writing the value when a combobox value is selected, but won't display the 0,"" value when I change the Combo value.

     

    Any help greatly appreciated:

     

    <ComboBox x:Name="cmbList" Margin="5,0" ItemsSource="{Binding ListOfValues}" DisplayMemberPath="Value">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged" SourceObject="{Binding ElementName=cmbList}">
               <i:InvokeCommandAction Command="{Binding SelectionChangedCombo}" CommandParameter="{Binding ElementName=cmbList, Path=SelectedItem}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ComboBox>
    Saturday, August 13, 2011 3:44 AM

Answers

  • Bind the ComboBox's SelectedItem to a property 'MySelectedItem' on your ViewModel and ensure the binding's Mode=TwoWay.  Set the ViewModel's 'MySelectedItem' = null via event or commandbinding to clear the selected item.


    Michael Stacie
    Saturday, August 13, 2011 1:45 PM

All replies

  • Bind the ComboBox's SelectedItem to a property 'MySelectedItem' on your ViewModel and ensure the binding's Mode=TwoWay.  Set the ViewModel's 'MySelectedItem' = null via event or commandbinding to clear the selected item.


    Michael Stacie
    Saturday, August 13, 2011 1:45 PM
  • That is good and is working, but it broke my delegate command. Hmmm will look into that thanks!
    Saturday, August 13, 2011 3:48 PM
  • Welcome good luck!
    Michael Stacie
    Saturday, August 13, 2011 4:09 PM