If you want the item list to close automatically when the mouse is leaving the item list, without having to click outside the list, you can get a reference to the Popup element in the Combobox template and take control of its MouseLeave event. When you set the IsOpen to false, it will close automatically and leave the control of the mouse to the main window.
XAML:
<ComboBox Name="comboBox" Margin="5" Width="100" DropDownOpened="comboBox_DropDownOpened"/>
Code behind:
Popup popup = null;
private void comboBox_DropDownOpened(object sender, EventArgs e)
{
if (popup == null)
{
popup = (Popup)comboBox.Template.FindName("PART_Popup", comboBox);
popup.MouseLeave += Popup_MouseLeave;
}
}
private void Popup_MouseLeave(object sender, MouseEventArgs e)
{
((Popup)sender).IsOpen = false;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…