Skip to content

Commit 75d9819

Browse files
committed
Merge branch 'popup-button-fix' of https://github.com/jonfunkhouser/MaterialDesignInXamlToolkit into jonfunkhouser-popup-button-fix
2 parents a127a6b + bc99be6 commit 75d9819

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
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/ComboBoxPopup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private CustomPopupPlacement[] ComboBoxCustomPopupPlacementCallback(Size popupSi
106106
{
107107
var locationFromScreen = this.PlacementTarget.PointToScreen(new Point(0, 0));
108108

109-
var mainVisual = PlacementTarget.GetVisualAncestory().OfType<System.Windows.Media.Visual>().LastOrDefault();
109+
var mainVisual = PlacementTarget.GetVisualAncestry().OfType<System.Windows.Media.Visual>().LastOrDefault();
110110
if (mainVisual == null) return new CustomPopupPlacement[0];
111111

112112
var screenWidth = (int) DpiHelper.TransformToDeviceX(mainVisual, SystemParameters.PrimaryScreenWidth);

MaterialDesignThemes.Wpf/Extensions.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,46 @@ public static IEnumerable<DependencyObject> VisualDepthFirstTraversal(this Depen
2424
}
2525
}
2626

27-
public static bool IsDescendant(this DependencyObject parent, DependencyObject node)
27+
public static bool IsAncestorOf(this DependencyObject parent, DependencyObject node)
2828
{
2929
return node != null && parent.VisualDepthFirstTraversal().Contains(node);
3030
}
3131

3232
/// <summary>
33-
/// Returns full visual ancestory, starting at the leaf.
33+
/// Returns full visual ancestry, starting at the leaf.
3434
/// </summary>
3535
/// <param name="leaf"></param>
3636
/// <returns></returns>
37-
public static IEnumerable<DependencyObject> GetVisualAncestory(this DependencyObject leaf)
37+
public static IEnumerable<DependencyObject> GetVisualAncestry(this DependencyObject leaf)
3838
{
3939
while (leaf != null)
4040
{
4141
yield return leaf;
4242
leaf = VisualTreeHelper.GetParent(leaf);
4343
}
44-
}
44+
}
45+
46+
public static IEnumerable<DependencyObject> GetLogicalAncestry(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 IsDescendantOf(this DependencyObject leaf, DependencyObject ancestor)
56+
{
57+
DependencyObject parent = null;
58+
foreach (var node in leaf.GetVisualAncestry())
59+
{
60+
if (Equals(node, ancestor))
61+
return true;
62+
63+
parent = node;
64+
}
65+
66+
return parent != null && parent.GetLogicalAncestry().Contains(ancestor);
67+
}
4568
}
4669
}

MaterialDesignThemes.Wpf/PopupBox.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,14 +578,14 @@ 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).IsDescendantOf(popupBox._popup))
582582
{
583583
popupBox.Close();
584584
}
585585
}
586586
else
587587
{
588-
if ((Mouse.Captured as DependencyObject).GetVisualAncestory().Contains(popupBox._popup))
588+
if ((Mouse.Captured as DependencyObject).GetVisualAncestry().Contains(popupBox._popup))
589589
{
590590
// Take capture if one of our children gave up capture (by closing their drop down)
591591
if (popupBox.IsPopupOpen && Mouse.Captured == null && GetCapture() == IntPtr.Zero)

0 commit comments

Comments
 (0)