2
votes

I would like to create custom control that will look like standard WPF ComboBox, but instead of instead of having an ItemsPresenter in the popup there will be another custom control. So, I created a new class that derives from System.Windows.Controls.Control, added a IsDropDownOpen property and created a style that is actually a copy of default ComboBox style (main idea is that the Popup.IsOpen and ToggleButton.IsPressed properties are bound to the IsDropDownOpen property of the control).

The problem is that the Popup is not closed when I click outside of the control.

I took a look at the ComboBox class in the Reflector and found out that ComboBox used some logic to update the IsDropDownOpen property when it loses mouse capture. But that code uses some internal classes. Is there any alternative way to determine if the user clicked outside of the control and close the Popup?

UPD: I didn't find the way to attach a file to post, so I uploaded sample project here

There is a custom control that looks like ComboBox, but it has a TreeView in a popup. When you open popup and click outside of the control it closes automatically, but if you open popup, expand 'Item2' and then click outside the popup isn't closed. The question is how to fix this?

2
@RQDQ: What exactly do you need? Control template? As I said It's the same as default combobox template, but instead of ItemsPresenter there is an other control.adogg
The idea is we want to see what you already have so we don't have to reinvent the wheel just to help you.RQDQ
@RQDQ - I updated my original post with the link to the sample project and additional question.adogg

2 Answers

0
votes

There is the Control.LostFocus event, maybe handling that would be sufficient for this.

0
votes

This code solves the problem.

In the static contructor:

EventManager.RegisterClassHandler(typeof(CustomComboBox), Mouse.LostMouseCaptureEvent, new MouseEventHandler(OnMouseCaptureLost));

Event handler implementation:

private void OnMouseCaptureLost(object sender, MouseEventArgs e)
{
   if (Mouse.Captured != _container)
   {
      if (e.OriginalSource != _container)
      {
         Mouse.Capture(_container, CaptureMode.SubTree);
         e.Handled = true; 
      }
   }
}