Skip to content

Commit b3926fb

Browse files
committed
Merge branch 'ichepikov-feature/TreeHelper_improvements' into develop
2 parents 4979b7c + 6ddf3f8 commit b3926fb

File tree

5 files changed

+28
-49
lines changed

5 files changed

+28
-49
lines changed

src/MahApps.Metro/MahApps.Metro.Shared/Behaviours/ReloadBehavior.cs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
31
using System.Windows;
42
using System.Windows.Controls;
5-
using System.Windows.Media;
63
using MahApps.Metro.Controls;
74

85
namespace MahApps.Metro.Behaviours
@@ -54,32 +51,19 @@ private static void OnSelectedTabChanged(DependencyObject d, DependencyPropertyC
5451
((ContentControl)d).Loaded += ReloadLoaded;
5552
}
5653

57-
static void ReloadLoaded(object sender, RoutedEventArgs e)
54+
private static void ReloadLoaded(object sender, RoutedEventArgs e)
5855
{
59-
var metroContentControl = ((ContentControl)sender);
60-
var tab = Ancestors(metroContentControl)
61-
.OfType<TabControl>()
62-
.FirstOrDefault();
56+
var metroContentControl = (ContentControl)sender;
57+
var tab = metroContentControl.TryFindParent<TabControl>();
6358

6459
if (tab == null) return;
6560

6661
SetMetroContentControl(tab, metroContentControl);
6762
tab.SelectionChanged -= ReloadSelectionChanged;
6863
tab.SelectionChanged += ReloadSelectionChanged;
6964
}
70-
71-
private static IEnumerable<DependencyObject> Ancestors(DependencyObject obj)
72-
{
73-
var parent = VisualTreeHelper.GetParent(obj);
74-
while (parent != null)
75-
{
76-
yield return parent;
77-
obj = parent;
78-
parent = VisualTreeHelper.GetParent(obj);
79-
}
80-
}
81-
82-
static void ReloadSelectionChanged(object sender, SelectionChangedEventArgs e)
65+
66+
private static void ReloadSelectionChanged(object sender, SelectionChangedEventArgs e)
8367
{
8468
if (e.OriginalSource != sender)
8569
return;

src/MahApps.Metro/MahApps.Metro.Shared/Behaviours/TiltBehavior.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,7 @@ private void CompositionTargetRendering(object sender, EventArgs e)
172172

173173
private static Panel GetParentPanel(DependencyObject element)
174174
{
175-
var parent = VisualTreeHelper.GetParent(element);
176-
var panel = parent as Panel;
177-
178-
return panel ?? (parent == null ? null : GetParentPanel(parent));
175+
return element.TryFindParent<Panel>();
179176
}
180177
}
181178
}

src/MahApps.Metro/MahApps.Metro.Shared/Controls/Helper/TextBoxHelper.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -886,13 +886,10 @@ private static void IsClearTextButtonBehaviorEnabledChanged(DependencyObject d,
886886

887887
public static void ButtonClicked(object sender, RoutedEventArgs e)
888888
{
889-
var button = ((Button)sender);
890-
var parent = VisualTreeHelper.GetParent(button);
891-
while (!(parent is TextBox || parent is PasswordBox || parent is ComboBox))
892-
{
893-
parent = VisualTreeHelper.GetParent(parent);
894-
}
889+
var button = (Button)sender;
895890

891+
var parent = button.GetAncestors().FirstOrDefault(a => a is TextBox || a is PasswordBox || a is ComboBox);
892+
896893
var command = GetButtonCommand(parent);
897894
var commandParameter = GetButtonCommandParameter(parent) ?? parent;
898895
if (command != null && command.CanExecute(commandParameter))

src/MahApps.Metro/MahApps.Metro.Shared/Controls/TreeHelper.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ public static T TryFindParent<T>(this DependencyObject child)
3535
return parent ?? TryFindParent<T>(parentObject);
3636
}
3737

38+
/// <summary>
39+
/// Finds all Ancestors of a given item on the visual tree.
40+
/// </summary>
41+
/// <param name="child">A node in a visual tree</param>
42+
/// <returns>All ancestors in visual tree of <paramref name="child"/> element</returns>
43+
public static IEnumerable<DependencyObject> GetAncestors(this DependencyObject child)
44+
{
45+
var parent = VisualTreeHelper.GetParent(child);
46+
while (parent != null)
47+
{
48+
yield return parent;
49+
parent = VisualTreeHelper.GetParent(parent);
50+
}
51+
}
52+
3853
/// <summary>
3954
/// Finds a Child of a given item in the visual tree.
4055
/// </summary>

src/MahApps.Metro/MahApps.Metro.Shared/Converters/TreeViewMarginConverter.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using System;
1+
using MahApps.Metro.Controls;
2+
using System;
23
using System.Globalization;
4+
using System.Linq;
35
using System.Windows;
46
using System.Windows.Controls;
57
using System.Windows.Data;
6-
using System.Windows.Media;
78

89
namespace MahApps.Metro.Converters
910
{
@@ -30,22 +31,7 @@ public static class TreeViewItemExtensions
3031
{
3132
public static int GetDepth(this TreeViewItem item)
3233
{
33-
TreeViewItem parent;
34-
while ((parent = GetParent(item)) != null)
35-
{
36-
return GetDepth(parent) + 1;
37-
}
38-
return 0;
39-
}
40-
41-
private static TreeViewItem GetParent(TreeViewItem item)
42-
{
43-
var parent = item != null ? VisualTreeHelper.GetParent(item) : null;
44-
while (parent != null && !(parent is TreeViewItem || parent is TreeView))
45-
{
46-
parent = VisualTreeHelper.GetParent(parent);
47-
}
48-
return parent as TreeViewItem;
34+
return item.GetAncestors().TakeWhile(e => !(e is TreeView)).OfType<TreeViewItem>().Count();
4935
}
5036
}
5137
}

0 commit comments

Comments
 (0)