Skip to content

Commit

Permalink
* Rename FindElementOfTypeInSubtree -> FindVisualChildByType (#3438)
Browse files Browse the repository at this point in the history
* FindVisualChildByName() now an extension method

* internal code cleanup
  • Loading branch information
Felix-Dev authored Oct 29, 2020
1 parent 4becbf5 commit 4d7abe1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion dev/CommonStyles/TestUI/CommonStylesPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ private void TimePickerDensityTest_Click(object sender, RoutedEventArgs e)

private void ListViewItemDensityTest_Click(object sender, RoutedEventArgs e)
{
var item = ListView1.FindElementOfTypeInSubtree<ListViewItem>();
var item = ListView1.FindVisualChildByType<ListViewItem>();
SimpleVerify simpleVerify = new SimpleVerify();
if (item != null)
{
Expand Down
4 changes: 2 additions & 2 deletions dev/CommonStyles/TestUI/CompactPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ private FrameworkElement GetChild(FrameworkElement item, string childXName)
private void RunTest_Click(object sender, RoutedEventArgs e)
{
SimpleVerify simpleVerify = new SimpleVerify();
FrameworkElement item = ListView.FindElementOfTypeInSubtree<ListViewItem>();
FrameworkElement item = ListView.FindVisualChildByType<ListViewItem>();
VerifyHeight(simpleVerify, item, 32, "ListViewItem");
item = TreeView.FindElementOfTypeInSubtree<TreeViewItem>();
item = TreeView.FindVisualChildByType<TreeViewItem>();
VerifyHeight(simpleVerify, item, 24, "TreeViewItem");

VerifyHeight(simpleVerify, NavItem1, 32, "NavigationViewItem");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private void OnScrollButtonClicked(object sender, RoutedEventArgs e)

if (i < indexPath.Length - 1)
{
repeater = container.FindElementOfTypeInSubtree<ItemsRepeater>();
repeater = container.FindVisualChildByType<ItemsRepeater>();
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion dev/TabView/TestUI/TabViewTabItemsSourcePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private void TabViewItemsSourceSample_TabItemsChanged(TabView sender, IVectorCha

private void TabViewTabItemsSourcePage_Loaded(object sender, RoutedEventArgs e)
{
innerListView = TabViewItemsSourceSample.FindElementOfTypeInSubtree<ListView>();
innerListView = TabViewItemsSourceSample.FindVisualChildByType<ListView>();

for (int tabIndex = 0; tabIndex < initialTabsCount; tabIndex++)
{
Expand Down
32 changes: 15 additions & 17 deletions test/MUXControlsTestApp/Utilities/VisualTreeUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ namespace MUXControlsTestApp.Utilities
{
public static class VisualTreeUtils
{
public static T FindElementOfTypeInSubtree<T>(this DependencyObject element)
public static T FindVisualChildByType<T>(this DependencyObject element)
where T : DependencyObject
{
if (element == null)
{
return null;
}

if (element is T)
if (element is T elementAsT)
{
return (T)element;
return elementAsT;
}

int childrenCount = VisualTreeHelper.GetChildrenCount(element);
for (int i = 0; i < childrenCount; i++)
{
var result = FindElementOfTypeInSubtree<T>(VisualTreeHelper.GetChild(element, i));
var result = VisualTreeHelper.GetChild(element, i).FindVisualChildByType<T>();
if (result != null)
{
return result;
Expand All @@ -34,27 +34,25 @@ public static T FindElementOfTypeInSubtree<T>(this DependencyObject element)
return null;
}

public static DependencyObject FindVisualChildByName(FrameworkElement parent, string name)
public static FrameworkElement FindVisualChildByName(this DependencyObject element, string name)
{
if (parent.Name == name)
if (element == null || string.IsNullOrWhiteSpace(name))
{
return parent;
return null;
}

int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
if (element is FrameworkElement elementAsFE && elementAsFE.Name == name)
{
return elementAsFE;
}

int childrenCount = VisualTreeHelper.GetChildrenCount(element);
for (int i = 0; i < childrenCount; i++)
{
FrameworkElement childAsFE = VisualTreeHelper.GetChild(parent, i) as FrameworkElement;

if (childAsFE != null)
var result = VisualTreeHelper.GetChild(element, i).FindVisualChildByName(name);
if (result != null)
{
DependencyObject result = FindVisualChildByName(childAsFE, name);

if (result != null)
{
return result;
}
return result;
}
}

Expand Down

0 comments on commit 4d7abe1

Please sign in to comment.