开发者

WPF combobox-like custom control

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 IsD开发者_如何转开发ropDownOpen 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?


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


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; 
      }
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜