-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataGridMiscHelpers.cs
79 lines (74 loc) · 2.89 KB
/
DataGridMiscHelpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Media3D;
namespace MarlinEasyConfig
{
public static class DataGridMiscHelpers
{
/// <summary>
/// Finds the visual parent of the given object
/// </summary>
/// <param name="originalSource">The original source.</param>
/// <returns></returns>
public static DependencyObject FindVisualParentAsDataGridSubComponent(
DependencyObject originalSource)
{
// iteratively traverse the visual tree
while ((originalSource != null)
&& !(originalSource is DataGridCell)
&& !(originalSource is DataGridColumnHeader)
&& !(originalSource is DataGridRow))
{
if (originalSource is Visual || originalSource is Visual3D)
{
originalSource = VisualTreeHelper.GetParent(originalSource);
}
else
{
// If we're in Logical Land then we must walk
// up the logical tree until we find a
// Visual/Visual3D to get us back to Visual Land.
originalSource = LogicalTreeHelper.GetParent(originalSource);
}
}
return originalSource;
}
/// <summary>
/// Finds the cell and row data for the given source.
/// </summary>
/// <param name="originalSource">The original source.</param>
/// <param name="cell">The cell.</param>
/// <param name="row">The row.</param>
public static void FindCellAndRow(DependencyObject originalSource,
out DataGridCell cell, out DataGridRow row)
{
cell = originalSource as DataGridCell;
if (cell == null)
{
row = null;
return;
}
// Walk the visual tree to find the cell's parent row.
while ((originalSource != null) && !(originalSource is DataGridRow))
{
if (originalSource is Visual || originalSource is Visual3D)
{
originalSource = VisualTreeHelper.GetParent(originalSource);
}
else
{
// If we're in Logical Land then we must walk up the logical tree
// until we find a Visual/Visual3D to get us back to Visual Land.
// See: http://www.codeproject.com/Articles/21495/Understanding-the-Visual-Tree-and-Logical-Tree-in
originalSource = LogicalTreeHelper.GetParent(originalSource);
}
}
row = originalSource as DataGridRow;
}
}
}