Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit d418380

Browse files
author
bytecode77
committed
* CSharp.FindDataContext -> UIContext.Find
1 parent 8ba60bc commit d418380

3 files changed

Lines changed: 28 additions & 18 deletions

File tree

BytecodeApi.UI/BytecodeApi.UI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
</Compile>
135135
<Compile Include="SingleInstanceManager.cs" />
136136
<Compile Include="Extensions\DispatcherObjectExtensions.cs" />
137+
<Compile Include="UIContext.cs" />
137138
<Compile Include="UITreeType.cs" />
138139
</ItemGroup>
139140
<ItemGroup>

BytecodeApi.UI/UIContext.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Windows;
2+
3+
namespace BytecodeApi.UI
4+
{
5+
/// <summary>
6+
/// Helper class to retrieve the DataContext property from <see cref="FrameworkElement" /> and <see cref="FrameworkContentElement" /> objects.
7+
/// </summary>
8+
public static class UIContext
9+
{
10+
/// <summary>
11+
/// Returns the DataContext property, if the specified <see cref="object" /> instance is either a <see cref="FrameworkElement" /> or a <see cref="FrameworkContentElement" />. If found and can be casted to the specified type, the DataContext is returned, otherwise, <see langword="default" />(<typeparamref name="T" />).
12+
/// </summary>
13+
/// <typeparam name="T">The return type to cast the DataContext property to.</typeparam>
14+
/// <param name="obj">The <see cref="object" /> where the DataContext property looked for. This is typically a parameter from a WPF event handler.</param>
15+
/// <returns>
16+
/// <see cref="FrameworkElement.DataContext" />, if <paramref name="obj" /> is a <see cref="FrameworkElement" />;
17+
/// <see cref="FrameworkContentElement.DataContext" />, if <paramref name="obj" /> instance is a <see cref="FrameworkContentElement" />;
18+
/// otherwise, <see langword="default" />(<typeparamref name="T" />).
19+
/// </returns>
20+
public static T Find<T>(object obj)
21+
{
22+
if (obj is FrameworkElement frameworkElement && frameworkElement.DataContext is T dataContext1) return dataContext1;
23+
else if (obj is FrameworkContentElement frameworkContentElement && frameworkContentElement.DataContext is T dataContext2) return dataContext2;
24+
else return default;
25+
}
26+
}
27+
}

BytecodeApi/CSharp.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Diagnostics;
77
using System.Linq;
88
using System.Threading;
9-
using System.Windows;
109

1110
namespace BytecodeApi
1211
{
@@ -380,22 +379,5 @@ public static bool EqualsAll<T>(T obj, params T[] values)
380379
{
381380
return EqualsAll(obj, (IEnumerable<T>)values);
382381
}
383-
384-
/// <summary>
385-
/// Returns the DataContext property, if the specified <see cref="object" /> instance is either a <see cref="FrameworkElement" /> or a <see cref="FrameworkContentElement" />. If found and can be casted to the specified type, the DataContext is returned, otherwise, <see langword="default" />(<typeparamref name="T" />).
386-
/// </summary>
387-
/// <typeparam name="T">The return type to cast the DataContext property to.</typeparam>
388-
/// <param name="obj">The <see cref="object" /> where the DataContext property looked for. This is typically a parameter from a WPF event handler.</param>
389-
/// <returns>
390-
/// <see cref="FrameworkElement.DataContext" />, if <paramref name="obj" /> is a <see cref="FrameworkElement" />;
391-
/// <see cref="FrameworkContentElement.DataContext" />, if <paramref name="obj" /> instance is a <see cref="FrameworkContentElement" />;
392-
/// otherwise, <see langword="default" />(<typeparamref name="T" />).
393-
/// </returns>
394-
public static T FindDataContext<T>(object obj)
395-
{
396-
if (obj is FrameworkElement frameworkElement && frameworkElement.DataContext is T dataContext1) return dataContext1;
397-
else if (obj is FrameworkContentElement frameworkContentElement && frameworkContentElement.DataContext is T dataContext2) return dataContext2;
398-
else return default;
399-
}
400382
}
401383
}

0 commit comments

Comments
 (0)