forked from ygoe/MultiSelectTreeView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFocusHelper.cs
More file actions
75 lines (67 loc) · 2.04 KB
/
Copy pathFocusHelper.cs
File metadata and controls
75 lines (67 loc) · 2.04 KB
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
using System.Threading;
using System.Windows.Threading;
namespace System.Windows.Controls
{
/// <summary>
/// Helper methods to focus.
/// </summary>
public static class FocusHelper
{
#region Public methods
public static void Focus(EditTextBox element)
{
//System.Diagnostics.Debug.WriteLine("Focus textbox with helper:" + element.Text);
FocusCore(element);
element.BringIntoView();
}
public static void Focus(MultiSelectTreeViewItem element, bool bringIntoView = false)
{
//System.Diagnostics.Debug.WriteLine("FocusHelper focusing " + (bringIntoView ? "[into view] " : "") + element.DataContext);
FocusCore(element);
if (bringIntoView)
{
FrameworkElement itemContent = (FrameworkElement) element.Template.FindName("headerBorder", element);
if (itemContent != null) // May not be rendered yet...
{
itemContent.BringIntoView();
}
}
}
public static void Focus(MultiSelectTreeView element)
{
//System.Diagnostics.Debug.WriteLine("Focus Tree with helper");
FocusCore(element);
element.BringIntoView();
}
private static void FocusCore(FrameworkElement element)
{
//System.Diagnostics.Debug.WriteLine("Focusing element " + element.ToString());
//System.Diagnostics.Debug.WriteLine(Environment.StackTrace);
if (!element.Focus())
{
//System.Diagnostics.Debug.WriteLine("- Element could not be focused, invoking in dispatcher thread");
element.Dispatcher.BeginInvoke(DispatcherPriority.Input, new ThreadStart(() => element.Focus()));
}
#if DEBUG
// no good idea, seems to block sometimes
int i = 0;
while (i < 5)
{
if (element.IsFocused)
{
//if (i > 0)
// System.Diagnostics.Debug.WriteLine("- Element is focused now in round " + i + ", leaving");
return;
}
Thread.Sleep(20);
i++;
}
//if (i >= 5)
//{
// System.Diagnostics.Debug.WriteLine("- Element is not focused after 500 ms, giving up");
//}
#endif
}
#endregion Public methods
}
}