Skip to content

Commit ab7c8a4

Browse files
authored
Merge pull request #258 from w-ahmad/chore/remove-CommunityToolkit.WinUI.Behaviors
chore: Removed CommunityToolkit.WinUI.Behaviors dependency
2 parents 6b6f0c2 + ecdb1e6 commit ab7c8a4

14 files changed

Lines changed: 255 additions & 51 deletions
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using Microsoft.UI.Xaml;
2+
using Microsoft.UI.Xaml.Media;
3+
using System;
4+
using System.Collections.Generic;
5+
6+
namespace WinUI.TableView.Extensions;
7+
8+
/// <summary>
9+
/// Provides extension methods for traversing the visual tree of <see cref="DependencyObject"/>.
10+
/// </summary>
11+
internal static class DependencyObjectExtensions
12+
{
13+
/// <summary>
14+
/// Finds the first descendant of the specified type <typeparamref name="T"/> that matches the optional predicate.
15+
/// </summary>
16+
/// <typeparam name="T">The type of descendant to find.</typeparam>
17+
/// <param name="element">The root element to search from.</param>
18+
/// <param name="predicate">An optional predicate to filter descendants.</param>
19+
/// <returns>The first matching descendant, or <c>null</c> if none found.</returns>
20+
internal static T? FindDescendant<T>(this DependencyObject element, Func<T, bool>? predicate = default) where T : DependencyObject
21+
{
22+
foreach (var descendant in element.FindDescendants())
23+
{
24+
if (descendant is T tDescendant && (predicate?.Invoke(tDescendant) ?? true))
25+
{
26+
return tDescendant;
27+
}
28+
}
29+
30+
return default;
31+
}
32+
33+
/// <summary>
34+
/// Enumerates all descendants of the specified <see cref="DependencyObject"/> in the visual tree.
35+
/// </summary>
36+
/// <param name="element">The root element to enumerate from.</param>
37+
/// <returns>An enumerable of all descendant <see cref="DependencyObject"/>s.</returns>
38+
internal static IEnumerable<DependencyObject> FindDescendants(this DependencyObject element)
39+
{
40+
var childrenCount = VisualTreeHelper.GetChildrenCount(element);
41+
42+
for (var i = 0; i < childrenCount; i++)
43+
{
44+
var child = VisualTreeHelper.GetChild(element, i);
45+
46+
yield return child;
47+
48+
foreach (var childOfChild in FindDescendants(child))
49+
{
50+
yield return childOfChild;
51+
}
52+
}
53+
}
54+
55+
/// <summary>
56+
/// Finds the first ascendant of the specified type <typeparamref name="T"/>.
57+
/// </summary>
58+
/// <typeparam name="T">The type of ascendant to find.</typeparam>
59+
/// <param name="element">The element to start searching from.</param>
60+
/// <returns>The first matching ascendant, or <c>null</c> if none found.</returns>
61+
internal static T? FindAscendant<T>(this DependencyObject element) where T : DependencyObject
62+
{
63+
foreach (var ascendant in element.FindAscendants())
64+
{
65+
if (ascendant is T tAscendant)
66+
{
67+
return tAscendant;
68+
}
69+
}
70+
71+
return default;
72+
}
73+
74+
/// <summary>
75+
/// Enumerates all ascendants of the specified <see cref="DependencyObject"/> in the visual tree.
76+
/// </summary>
77+
/// <param name="element">The element to start enumerating from.</param>
78+
/// <returns>An enumerable of all ascendant <see cref="DependencyObject"/>s.</returns>
79+
internal static IEnumerable<DependencyObject> FindAscendants(this DependencyObject element)
80+
{
81+
while (true)
82+
{
83+
var parent = VisualTreeHelper.GetParent(element);
84+
85+
if (parent is null)
86+
{
87+
yield break;
88+
}
89+
90+
yield return parent;
91+
92+
element = parent;
93+
}
94+
}
95+
}

src/Extensions/FrameworkElementExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using CommunityToolkit.WinUI;
2-
using Microsoft.UI.Xaml.Controls;
1+
using Microsoft.UI.Xaml.Controls;
32

43
namespace WinUI.TableView.Extensions;
54

src/Primitives/ListViewItemPresenter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using CommunityToolkit.WinUI;
2-
using Windows.Foundation;
1+
using Windows.Foundation;
2+
using WinUI.TableView.Extensions;
33

44
namespace WinUI.TableView.Primitives;
55

src/TableView.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using CommunityToolkit.WinUI;
21
using Microsoft.UI.Xaml;
32
using Microsoft.UI.Xaml.Controls;
43
using Microsoft.UI.Xaml.Controls.Primitives;

src/TableViewCell.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using CommunityToolkit.WinUI;
21
using Microsoft.UI;
32
using Microsoft.UI.Xaml;
43
using Microsoft.UI.Xaml.Controls;
@@ -10,6 +9,7 @@
109
using System.Linq;
1110
using System.Threading.Tasks;
1211
using Windows.Foundation;
12+
using WinUI.TableView.Extensions;
1313
using WinUI.TableView.Helpers;
1414

1515
namespace WinUI.TableView;

src/TableViewColumnHeader.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using CommunityToolkit.WinUI;
21
using Microsoft.UI;
32
using Microsoft.UI.Input;
43
using Microsoft.UI.Xaml;

src/TableViewHeaderRow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using CommunityToolkit.WinUI;
21
using Microsoft.UI;
32
using Microsoft.UI.Xaml;
43
using Microsoft.UI.Xaml.Controls;
@@ -13,6 +12,7 @@
1312
using System.Linq;
1413
using Windows.Foundation;
1514
using WinUI.TableView.Converters;
15+
using WinUI.TableView.Extensions;
1616

1717
namespace WinUI.TableView;
1818

src/TableViewRow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using CommunityToolkit.WinUI;
21
using Microsoft.UI.Xaml;
32
using Microsoft.UI.Xaml.Controls;
43
using Microsoft.UI.Xaml.Controls.Primitives;
@@ -13,6 +12,7 @@
1312
using System.Linq;
1413
using System.Threading.Tasks;
1514
using Windows.Foundation;
15+
using WinUI.TableView.Extensions;
1616
using WinUI.TableView.Helpers;
1717

1818
namespace WinUI.TableView;

src/TableViewRowPresenter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using CommunityToolkit.WinUI;
21
using Microsoft.UI;
32
using Microsoft.UI.Xaml;
43
using Microsoft.UI.Xaml.Controls;
@@ -10,6 +9,7 @@
109
using System.Collections.Generic;
1110
using System.Linq;
1211
using Windows.Foundation;
12+
using WinUI.TableView.Extensions;
1313

1414
namespace WinUI.TableView;
1515

src/Themes/TableView.xaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
44
xmlns:local="using:WinUI.TableView"
55
xmlns:helpers="using:WinUI.TableView.Helpers"
6-
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
7-
xmlns:behaviors="using:CommunityToolkit.WinUI.Behaviors"
86
xmlns:win="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
97
xmlns:not_win="http://uno.ui/not_win"
108
mc:Ignorable="not_win">

0 commit comments

Comments
 (0)