In Silverlight development, you can set the focus of a control. If the code is behind, you can directly set the focus () method of a control. But what if we use the mvvm mode to implement it through binding? For example, in a login page, you need to set the default focus of the logon name control tboxlogin (textbox. Through research, we found that textbox does not set the dependency attribute of focus. Therefore, direct binding cannot be implemented, and we can only implement it using other methods.
In Silverlight, there is a class library of system. Windows. interactivity. dll, which is used to handle Silverlight interaction functions. We can use behavior to implement the requirements here. For example, define a textboxfocusbehavior class, let it inherit from behavior <textbox>, define an additional attribute isfocused in it, and then set whether the textbox has focus by changing the attribute.
Textboxfocusbehavior
1 public class TextBoxFocusBehavior : Behavior<TextBox> 2 { 3 public static bool GetIsFocused(DependencyObject obj) 4 { 5 return (bool)obj.GetValue(IsFocusedProperty); 6 } 7 8 public static void SetIsFocused(DependencyObject obj, bool value) 9 {10 obj.SetValue(IsFocusedProperty, value);11 } 12 public static readonly DependencyProperty IsFocusedProperty =13 DependencyProperty.RegisterAttached("IsFocused", typeof(bool),14 typeof(TextBoxFocusBehavior), new PropertyMetadata((s, e) =>15 {16 var textBox = s as TextBox;17 if (textBox != null && e.NewValue is bool && (bool)e.NewValue)18 {19 textBox.Focus();20 }21 }));22 }
After defining the above class, you can bind a bool attribute in viewmodel to the isfocused additional attribute of textbox in XAML. The processing in XAML is similar to: <textbox text = "{binding loginname, mode = twoway, updatesourcetrigger = propertychanged }"
Loc: textboxfocusbehavior. isfocused = "{binding isfocused, mode = twoway}">
</Textbox> in this way, as long as the isfocused attribute in viewmodel changes, the textbox can be notified of whether it has focus.
Extended: here we are dealing with the focus settings of textbox. It is possible that we will also set the focus of other controls such as passwordbox or RichTextBox later. Therefore, we can abstract this behavior, so that a specific type of control needs to be set when the focus behavior is directly inherited from this abstract class. The implementation is as follows:
Controlfocusbehavior
1 public class ControlFocusBehavior<T> : Behavior<FrameworkElement> where T : Control 2 { 3 public static bool GetIsFocused(T obj) 4 { 5 return (bool)obj.GetValue(IsFocusedProperty); 6 } 7 8 public static void SetIsFocused(T obj, bool value) 9 {10 obj.SetValue(IsFocusedProperty, value);11 }12 13 public static readonly DependencyProperty IsFocusedProperty =14 DependencyProperty.RegisterAttached("IsFocused", typeof(bool),15 typeof(ControlFocusBehavior<T>), new PropertyMetadata((obj, e) =>16 {17 var element = obj as T;18 if (element != null && e.NewValue is bool && (bool)e.NewValue)19 {20 element.Focus();21 }22 }));23 }
With the abstract class above, we can handle it at this time. For example, the textbox above needs focus processing. This abstract class is used to implement a textbox.
Public class textboxfocusbehavior: controlfocusbehavior <textbox> {}
Same as passwordbox: public class passwordboxfocusbehavior: controlfocusbehavior <textbox> {}
In this way, our goal is achieved.
Note: After the Silverlight program is started, our first XAML does not actually get the focus. You need to set a line of code in the first XAML page to get the focus htmlpage by default. plugin. focus ();