OpenLightGroup Blog

rss

Blogs from OpenLightGroup.net


Windows Phone 7: Saving Data when Keyboard is visible

image

This blog post wont tell you how to save data when the keyboard is visible, but it will tell you how to avoid the problem.

image

If a user is entering something in a TextBox, and the keyboard is visible, If they click a Save button, it will not save what they are currently typing because the values they are typing have not been bound to the TextBox yet.

One way around this, is to disable the Save button whenever they are entering something in a TextBox.

image

First, give the control a name so you can access it programmatically.

Wire-up some event handlers for GotFocus and LostFocus:

 

        public ExpenseDetailsPage()
        {
            InitializeComponent();
            ApplicationBar = new ApplicationBar();
            ApplicationBar.IsVisible = true;
            ApplicationBar.IsMenuEnabled = true;
 
            ApplicationBarIconButton btnSaveTrip = new ApplicationBarIconButton(new Uri("/icons/appbar.save.rest.png", UriKind.Relative));
            btnSaveTrip.Text = "Save Trip";
            btnSaveTrip.Click += new EventHandler(btnSaveTrip_Click);
            ApplicationBar.Buttons.Add(btnSaveTrip);
 
            txtTripDetails.GotFocus += new RoutedEventHandler(TripEditing_GotFocus);
            txtTripDetails.LostFocus += new RoutedEventHandler(TripEditing_LostFocus);
        }

 

Then implement the methods to enable and disable the Save button:

 

        void TripEditing_LostFocus(object sender, RoutedEventArgs e)
        {
            var SaveButton =
                ApplicationBar.Buttons.Cast<iapplicationbariconbutton>().Where(X => X.Text == "Save Trip").FirstOrDefault();
 
            if (SaveButton != null)
            {
                SaveButton.IsEnabled = true;
            }
        }
 
        void TripEditing_GotFocus(object sender, RoutedEventArgs e)
        {
            var SaveButton = 
                ApplicationBar.Buttons.Cast<iapplicationbariconbutton>().Where(X => X.Text == "Save Trip").FirstOrDefault();
 
            if (SaveButton != null)
            {
                SaveButton.IsEnabled = false;
            }
        }




Comments are closed.
Showing 3 Comments
Avatar  Sergii Lutai 7 years ago

)))

Avatar  Michael Washington 7 years ago

@Ian - That is a good solution. Where were you 6 hours ago? Hahaha

Avatar  Ian T. Lackey 7 years ago

Good tip!&lt;br&gt;&lt;br&gt;I have also gotten around this by calling this.Focus(); inside of the SaveButton Click EventHandler. This forces the textbox to loose focus and which updates the bound property. &lt;br&gt;&lt;br&gt;Just thought I would throw that out there. The use case or story should determine which is the correct solution.