WPF - How Combobox Knows When to Close the Dropdown Menu

I want to write a control similar to ComboBox, and I wonder if anyone knows how to determine when the user clicks outside the borders ComboBox. ComboBoxcloses the drop-down menu in this situation.

+3
source share
2 answers

You can define your own control and management pattern using a text box and a popup. Then override the OnApplyTemplate method of your control and find the popup using:

var popup = this.GetTemplateChild("PART_Popup") as Popup;

, , IsOpen true false.

, - , PreviewMouseDown (- ):

var parent = VisualTreeHelper.GetParent(this) as UIElement;
if(parent != null) {
    parent.PreviewMouseDown += MouseDownHandler;
}

, . VisualTreeHelper.GetParent(...) , (, ) , .

, , , .

+2

Silverlight. Microsoft, , . WPF , Silverlight.

, . , RowSpan , . / / . , . 0. , Opacity = 0 . Red Opacity.25.

<Rectangle x:Name="fullScreenOverlay" Grid.RowSpan="3" Opacity="0" Fill="White" Visibility="Collapsed" MouseDown="FullScreenOverlay_OnMouseDown" />

XAML, XAML.

ComboBox, Visible, /MouseDown.

private void NotificationButton_Click(object sender, RoutedEventArgs e)
{
    fullScreenOverlay.Visibility = Visibility.Visible;
    notificationPopup.Visibility = Visibility.Visible;
}

, Rectangle , ComboBox, . .

private void FullScreenOverlay_OnMouseDown(object sender, MouseButtonEventArgs e)
{
    fullScreenOverlay.Visibility = Visibility.Collapsed;
    notificationPopup.Visibility = Visibility.Collapsed;
}

, , Microsoft ComboBox. . , , .

+2

Source: https://habr.com/ru/post/1717705/


All Articles