Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to do it in a PRISM or MVVM way (get rid of the code-behind code), you can implement a behavior. In my case I focus the view in the username field if it's empty and in the password if it's set (hence the 2 parameters).</p> <p>My implementation:</p> <pre><code>public static class ControlTextFocusBehavior { public static readonly DependencyProperty FocusParameterProperty = DependencyProperty.RegisterAttached( "FocusParameter", typeof(string), typeof(ControlTextFocusBehavior), new PropertyMetadata(OnSetFocusParameterCallBack)); public static readonly DependencyProperty IsEmptyFocusedProperty = DependencyProperty.RegisterAttached( "IsEmptyFocused", typeof(bool), typeof(ControlTextFocusBehavior), new PropertyMetadata(true)); private static void OnSetFocusParameterCallBack(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { Control control = dependencyObject as Control; if (control != null) { control.Loaded += new RoutedEventHandler(control_Loaded); } } private static void control_Loaded(object sender, RoutedEventArgs e) { Control control = sender as Control; control.Loaded -= new RoutedEventHandler(control_Loaded); DependencyObject dependencyObject = sender as DependencyObject; if (dependencyObject != null) { bool isEmptyFocused = GetIsEmptyFocused(dependencyObject); bool isNullOrEmpty = string.IsNullOrEmpty(GetFocusParameter(dependencyObject)); if ((isEmptyFocused &amp;&amp; isNullOrEmpty) || (!isEmptyFocused &amp;&amp; !isNullOrEmpty)) { HtmlPage.Plugin.Focus(); control.Focus(); } } } public static void SetFocusParameter(DependencyObject dependencyObject, string parameter) { dependencyObject.SetValue(FocusParameterProperty, parameter); } public static string GetFocusParameter(DependencyObject dependencyObject) { return dependencyObject.GetValue(FocusParameterProperty) as string; } public static void SetIsEmptyFocused(DependencyObject dependencyObject, bool parameter) { dependencyObject.SetValue(IsEmptyFocusedProperty, parameter); } public static bool GetIsEmptyFocused(DependencyObject dependencyObject) { return (bool)dependencyObject.GetValue(IsEmptyFocusedProperty); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload