Skip to content

Commit 0172aae

Browse files
committed
don't close the popupbox when mouse capture is lost to a descendant of the popupbox
1 parent 12533c3 commit 0172aae

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

MainDemo.Wpf/MainWindow.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
<ListBoxItem>Hello World</ListBoxItem>
170170
<ListBoxItem>Nice Popup</ListBoxItem>
171171
<ListBoxItem>Goodbye.</ListBoxItem>
172+
<ListBoxItem><Button x:Name="PopupButton" Content="Button" Click="PopupButton_OnClick"/></ListBoxItem>
172173
</ListBox>
173174
</materialDesign:PopupBox>
174175
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="22">Material Design In XAML Toolkit</TextBlock>

MainDemo.Wpf/MainWindow.xaml.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@ private void UIElement_OnPreviewMouseLeftButtonUp(object sender, MouseButtonEven
1717
{
1818
MenuToggleButton.IsChecked = false;
1919
}
20+
21+
private void PopupButton_OnClick(object sender, RoutedEventArgs e)
22+
{
23+
MessageBox.Show("PopupButton Clicked");
24+
}
2025
}
2126
}

MaterialDesignThemes.Wpf/Extensions.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ public static IEnumerable<DependencyObject> GetVisualAncestory(this DependencyOb
4141
yield return leaf;
4242
leaf = VisualTreeHelper.GetParent(leaf);
4343
}
44-
}
44+
}
45+
46+
public static IEnumerable<DependencyObject> GetLogicalAncestory(this DependencyObject leaf)
47+
{
48+
while (leaf != null)
49+
{
50+
yield return leaf;
51+
leaf = LogicalTreeHelper.GetParent(leaf);
52+
}
53+
}
54+
55+
public static bool HasAncestor(this DependencyObject leaf, DependencyObject ancestor)
56+
{
57+
var visualAncestry = leaf.GetVisualAncestory().ToArray();
58+
if (visualAncestry.Contains(ancestor))
59+
{
60+
return true;
61+
}
62+
63+
var lastVisualAncestor = visualAncestry.LastOrDefault();
64+
var logicalAncestry = GetLogicalAncestory(lastVisualAncestor);
65+
return logicalAncestry.Contains(ancestor);
66+
}
4567
}
4668
}

MaterialDesignThemes.Wpf/PopupBox.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ private static void OnLostMouseCapture(object sender, MouseEventArgs e)
578578
{
579579
if (e.OriginalSource == popupBox)
580580
{
581-
if (Mouse.Captured == null || popupBox._popup == null || !(Mouse.Captured as DependencyObject).GetVisualAncestory().Contains(popupBox._popup))
581+
if (Mouse.Captured == null || popupBox._popup == null || !(Mouse.Captured as DependencyObject).HasAncestor(popupBox._popup))
582582
{
583583
popupBox.Close();
584584
}

0 commit comments

Comments
 (0)