One key UX concept when working with the WP7 is making data entry easy for the user. When entering a text box on WP7 you are presented with the virtual keyboard (aka SIP – Soft Input Panel). The trick is allowing the user to close this keyboard without having to do something special (yes using the back button will close the keyboard, but this may not be as intuitive to the users)
The first thing we should look at is how to close the SIP (this technique comes from here). The basic idea around dismissing the keyboard is to add an event handler and check for certain key presses occurring.
How Code to close the keyboard
// Binding which registered the KeyDown event
≶TextBox Name="RegistrationKeyTextBox" Text="{Binding RegistrationKey, Mode=TwoWay}" Margin="12,0" KeyDown="RegistrationKeyTextBox_KeyDown"/>
// Event Handler
private void RegistrationKeyTextBox_KeyDown( object sender, KeyEventArgs e )
{
if ( e.Key == Key.Enter )
{
// because binding does not occure untile you leave the box we want to force it here
( (TextBox)sender ).ForceBinding();
// swap focus back to the screen
Focus();
ViewModel<DeviceRegistrationViewModel>().RegisterDeviceCommand.Execute( null );
}
}
Take notice in the code above the extension method .ForceBinding() this is the code we can use to force the binding to fire on the text box. You can also notice that I am calling into my ViewModel to execute the ‘Register Device’ actions.
In order to invoke the binding you will need the code below (hey, there may be other ways but this works)
Code to invoke binding
public static class TextBoxExtensions
{
public static void ForceBinding( this TextBox textBox )
{
var binding = textBox.GetBindingExpression( TextBox.TextProperty );
if ( binding != null ) { binding.UpdateSource(); }
}
}
The above we are simply calling the updateSource method which fires the binding.
As you can see you do have to provide a bit of coding (which sucks) to get this type of user experience but IMO it is well worth it.
Till next time,
*** If there is a better way I could not find it online ***
Posted
12-20-2010 10:25 AM
by
Derik Whittaker