-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extensions_GUI.cs
92 lines (89 loc) · 3.25 KB
/
Extensions_GUI.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
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.ComponentModel;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Windows;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
namespace ExtensionMethods
{
public static class UIExtensions
{
/// <summary>
/// UI Child finder
/// http://stackoverflow.com/questions/636383/wpf-ways-to-find-controls/1501391#1501391
/// e.g. : comboBox.FindChild(typeof(TextBox), "PART_EditableTextBox") as TextBox;
/// http://stackoverflow.com/questions/2151285/wpf-selecting-all-the-text-in-and-setting-focus-to-a-comboboxs-editable-textbo
/// </summary>
/// <param name="reference"></param>
/// <param name="childName"></param>
/// <param name="childType"></param>
/// <returns></returns>
public static DependencyObject FindChild(this DependencyObject reference, Type childType, string childName)
{
DependencyObject foundChild = null;
if (reference != null)
{
int childrenCount = VisualTreeHelper.GetChildrenCount(reference);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(reference, i);
// If the child is not of the request child type child
if (child.GetType() != childType)
{
// recursively drill down the tree
foundChild = FindChild(child, childType, childName);
}
else if (!string.IsNullOrEmpty(childName))
{
var frameworkElement = child as FrameworkElement;
// If the child's name is set for search
if (frameworkElement != null && frameworkElement.Name == childName)
{
// if the child's name is of the request name
foundChild = child;
break;
}
}
else
{
// child element found.
foundChild = child;
break;
}
}
}
return foundChild;
}
}
public static class ListBoxExtensions
{
public static ScrollViewer GetScrollViewer(this ListBox listBox)
{
Border scroll_border = VisualTreeHelper.GetChild(listBox, 0) as Border;
if (scroll_border is Border)
{
ScrollViewer scroll = scroll_border.Child as ScrollViewer;
if (scroll is ScrollViewer)
{
return scroll;
}
else
{
return null;
}
}
else
{
return null;
}
}
}
}