-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensured the search box is focused when the dialog is opened.
- Loading branch information
Showing
2 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
source/ProjectFilter/UI/AttachedProperties/FocusWhenVisible.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using Microsoft.VisualStudio; | ||
using System; | ||
using System.Windows; | ||
|
||
|
||
namespace ProjectFilter.UI.AttachedProperties { | ||
|
||
public static class FocusWhenVisible { | ||
|
||
public static readonly DependencyProperty EnabledProperty = DependencyProperty.RegisterAttached( | ||
"Enabled", | ||
typeof(bool), | ||
typeof(FocusWhenVisible), | ||
new PropertyMetadata(false, OnEnabledChanged) | ||
); | ||
|
||
|
||
public static bool GetEnabled(DependencyObject obj) { | ||
if (obj is null) { | ||
throw new ArgumentNullException(nameof(obj)); | ||
} | ||
|
||
return (bool)obj.GetValue(EnabledProperty); | ||
} | ||
|
||
|
||
public static void SetEnabled(DependencyObject obj, bool value) { | ||
if (obj is null) { | ||
throw new ArgumentNullException(nameof(obj)); | ||
} | ||
|
||
obj.SetValue(EnabledProperty, value); | ||
} | ||
|
||
|
||
private static void OnEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { | ||
UIElement? target; | ||
|
||
|
||
target = d as UIElement; | ||
|
||
if (target != null) { | ||
if ((bool)e.OldValue) { | ||
target.IsVisibleChanged -= OnIsVisibleChanged; | ||
} | ||
|
||
if ((bool)e.NewValue) { | ||
target.IsVisibleChanged += OnIsVisibleChanged; | ||
} | ||
} | ||
} | ||
|
||
|
||
private static void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { | ||
UIElement? target; | ||
|
||
|
||
target = sender as UIElement; | ||
|
||
if (target != null && target.IsVisible) { | ||
try { | ||
target.Focus(); | ||
} catch (Exception ex) when (!ErrorHandler.IsCriticalException(ex)) { | ||
System.Diagnostics.Debug.WriteLine(ex); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters