Framework: Silverlight 4
I have a simple ChildWindow
with Username TextBox
and Password PasswordBox
. I have attached an event handler to the window's KeyDown
event.
private void onKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Escape) cancelButtonClick(null, null); if (e.Key == Key.Enter) okButtonClick(null, null); }
What I'm trying to achieve is that when the user presses Enter key on the keyboard, the program will behave as if the user had clicked the OK button.
The problem is the validation.
The default behavior for Silverlight's PasswordBox is to perform validation when the control loses focus. My PasswordBox
is bound to some User
object. When I click the Enter button, the event handler gets called, which in turn calls the okButtonClick(null, null)
. The problem is that the PasswordBox
has not yet lost the focus at that time, so the user.Password
property, which the PasswordBox
is bound to, is still empty.
I've tried to place btnOK.Focus()
before the okButtonClick(null, null)
but to no avail.
How to set the binding so that the control will update binding on every text change instead of on the LostFocus
event? What is the right way to achieve what I need?
Answer: 1
I have solved the problem by stumbling upon this solution.
It basicly uses attached property to subscribe to the TextChanged/PasswordChnaged events, and then in the event it updates the binding source. After adapting the solution to my needs, here is what I got (and it works flawlessly):
public class BindingHelper { public static readonly DependencyProperty RefreshOnChangeProperty = DependencyProperty.RegisterAttached("RefreshOnChange", typeof(bool), typeof(BindingHelper), new PropertyMetadata(false, OnRefreshOnChangeChanged)); public static void SetRefreshOnChange(DependencyObject o, bool value) { o.SetValue(RefreshOnChangeProperty, value); } public static bool GetRefreshOnChange(DependencyObject o) { return (bool)o.GetValue(RefreshOnChangeProperty); } private static void OnRefreshOnChangeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { if ((obj as TextBox) != null) { if ((bool)e.NewValue) (obj as TextBox).TextChanged += textBox_TextChanged; else (obj as TextBox).TextChanged -= textBox_TextChanged; } if ((obj as PasswordBox) != null) { if ((bool)e.NewValue) (obj as PasswordBox).PasswordChanged += passwordBox_PasswordChanged; else (obj as PasswordBox).PasswordChanged -= passwordBox_PasswordChanged; } } static void passwordBox_PasswordChanged(object sender, RoutedEventArgs e) { PasswordBox passwordBox = sender as PasswordBox; if (passwordBox != null) { BindingExpression binding = passwordBox.GetBindingExpression(PasswordBox.PasswordProperty); if (binding != null) binding.UpdateSource(); } } static void textBox_TextChanged(object sender, TextChangedEventArgs e) { TextBox textBox = sender as TextBox; if (textBox != null) { BindingExpression binding = textBox.GetBindingExpression(TextBox.TextProperty); if (binding != null) binding.UpdateSource(); } } }
by : Kornelije Petakhttp://stackoverflow.com/users/93770Answer: 2
Use PasswordChanged
event for Passwordbox
.
Textbox
has TextboxChanged
event.
No comments:
Post a Comment
Send us your comment related to the topic mentioned on the blog