0

I am using a WPF Combobox and performing operations on the SelectionChanged event. I'm also changing the collections from code-behind and do not want the code in the SelectionChanged event to be triggered then. As such, I am using the IsDropDownOpen property to determine if the dropdown source was changed using the code or by a user from the application. This works fine when I use the mouse to select values in the dropdown, however, if I use the keyboard to select values and press the Enter key, the event is fired but the IsDropDownOpen property is false. I am therefore unable to correctly determine whether the dropdown selection is changed using the code or the application.

The code is as below.

<ComboBox SelectionChanged="Company_SelectionChanged"
DisplayMemberPath="Name"  Tag="OrgGroupId" 
ItemsSource="{Binding CompanyCollection,UpdateSourceTrigger=PropertyChanged}"
SelectedValue="{Binding SelectedCompany,UpdateSourceTrigger=PropertyChanged}"
x:Name="cmbCompany" />

The selection changed even is as below.

private void Company_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (cmbCompany.IsDropDownOpen)
    {
        /*do something here*/
    }
}

How can I correctly determine when the dropdown selection is changed using the application and block the code from executing when it is changed through code?

UPDATE

I managed to get the code working by adding a check for the IsSelectionBoxHighlighted property. This property returns true whenever a user is using the dropdown to select values, irrespective of whether a mouse or a keyboard is used. The modified code is as below.

private void Company_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (cmbCompany.IsDropDownOpen || cmbCompany.IsSelectionBoxHighlighted)
    {
        /*do something here*/
    }
}

I could not find a lot of information on the IsSelectionBoxHighlighted property online. Can this lead to other errors? Or are there any particular scenarios when this property is set/reset?

Fahad
  • 1,364
  • 3
  • 20
  • 40
  • possible duplicate of [Determine whether Selector.SelectionChanged event was initiated by a user](http://stackoverflow.com/questions/7412280/determine-whether-selector-selectionchanged-event-was-initiated-by-a-user) – Oren Hizkiya Mar 28 '14 at 16:43
  • @Oren As I've mentioned, I'm already using the IsDropDownOpen condition check, which does not work if the user uses the keyboard Enter key to select a value. – Fahad Mar 28 '14 at 16:52
  • Sounds like you're going to want to use `OnSelectionChanged` possibly. – Kcvin Mar 28 '14 at 16:57
  • if you're expecting the drop down to be open whenever the user changes the selected item, then isn't that if statement useless? – Kcvin Mar 28 '14 at 17:04
  • @NETscape the statement works if I change anything through code, the dropdown will always be closed at that instant. I thought this was the solution to my problem, however, I did not consider that the user may use only the keyboard, this would fail then. – Fahad Mar 28 '14 at 17:10

1 Answers1

0

How about creating a field to track it yourself.

bool _applicationChangingDropdown = false;

Set it to true at the start of any code that you don't want to trigger the SelectionChanged and set it to false after.

Then just use

if(!_applicationChangingDropdown)
{
}
Jason Hunt
  • 354
  • 2
  • 5