Search code examples
wpfxamlz-order

WPF ComboBox is not collapsing dropdown once it loses focus


I have a very strange situation. I have ComboBox which is loaded with some items. When I click on it, the drop-down expands showing all items. If I now open another application such as Notepad, the dropdown will not collapse which I would expect to happen. As a result, any overlapping application, such as opened Notepad, will appear as being located in between the WPF hosting the ComboBox and the ComboBox drop-down like on screenshot below. Probably the z-order is somehow totally wrong.

I believe the reason for this behavior is the fact that the dropdown did not collapse at the first place. My ComboBox is part of a Usercontrol and its xaml is like:

    <ComboBox ItemsSource="{Binding ValueSet}"
              SelectedItem="{Binding SelectedNode}"
              DisplayMemberPath="Name"
              BorderBrush="Green" 
              BorderThickness="1" SelectionChanged="ComboBox_SelectionChanged">
    </ComboBox>

enter image description here


Solution

  • In my opinion there is no any problem with the z-order in your xaml. The actual problem is in the ComboBox template in your application. You should find the actual style(or control template) your Combo is based on and check the Popup(usually named "PART_Popup") control inside that control template. The IsOpen property of the Popup should be bound to the ComboBox IsDropDownOpened property, like that:

    `IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}"`
    

    Any way you should search the solution for your problem in incorrect control template of the Combo control. There is something that make it be opened(probably by making the IsOpen property of the inner Popup control to be always true). Let me know if it you need more suggestion.