The problem of the Windows Phone 7 ApplicationBar and not (yet) updated Bindings

As a MVVM enthusiast I’m trying to use MVVM as much as possible, but not for things that are almost impossible to do with MVVM. But simple things, like a TextBox Text property is always bound to a ViewModel’s property. That’s something that works very well…until you start having ApplicationBar buttons and menu items.

A lot of people already know that the ApplicationBar is something special, it’s not just a control like other stuff in your application. When clicking a button on the ApplicationBar the Binding is not yet updated. This is basically because the focus which is on the TextBox for example won’t be changed when clicking ApplicationBar buttons. This is different compared to normal buttons that you put somewhere on your page.

The Solution?

I’ve read different solutions on the web. For example, force focus on a different control by calling the Focus method on a Control. While this is working, I found a technically nicer solution. By getting the Binding of the current focused control and updating that binding.

I wrote a small helper class that implements this Binding updating.

public static class ApplicationBarHelper
{
    public static void UpdateBindingOnFocussedControl()
    {
        object focusedElement = FocusManager.GetFocusedElement();
        if (focusedElement != null && focusedElement is TextBox)
        {
            var binding = (focusedElement as TextBox).GetBindingExpression(TextBox.TextProperty);
            if (binding != null)
                binding.UpdateSource();
        }
    } 
}

The first thing you do in the Event handlers for the ApplicationBar Click events is call.

ApplicationBarHelper.UpdateBindingOnFocussedControl();

What about the BindableApplicationBar?

A lot of use MVVM enthusiasts are using the BindableApplicationBar that’s part of the Phone7.Fx library. Can we use this in combination with the BindableApplicationBar? Yes we can, actually the best way would be to have this integrated with the BindableApplicationBar itself. I have just downloaded the source and manipulated two methods in two classes.

public class BindableApplicationBarIconButton : FrameworkElement, IApplicationBarIconButton
{
    // Other code that hasn't been changed in this class.

    void ApplicationBarIconButtonClick(object sender, EventArgs e)
    {
        ApplicationBarHelper.UpdateBindingOnFocussedControl();
        if (Command != null && CommandParameter != null)
            Command.Execute(CommandParameter);
        else if (Command != null)
            Command.Execute(CommandParameterValue);
        if (Click != null)
            Click(this, e);
    }
}

public class BindableApplicationBarMenuItem : FrameworkElement, IApplicationBarMenuItem
{
    // Other code that hasn't been changed in this class.

    private void ApplicationBarMenuItemClick(object sender, EventArgs e)
    {
        ApplicationBarHelper.UpdateBindingOnFocussedControl();
        if (Command != null && CommandParameter != null)
            Command.Execute(CommandParameter);
        else if (Command != null)
            Command.Execute(CommandParameterValue);
        if (Click != null)
            Click(this, e);
    }
}
  • Gravatar mbt prezzi May 5th, 2014 at 03:20
    bolstering your back muscles. In addition, your posture naturally becomes more vertical instead of leaning forward like the body is wont to do in regular shoes. Here, As i offer a short outline
  • Gravatar mbt meli May 5th, 2014 at 03:21
    right now just by downloading this spying software for cellphone on you own cmputer. You can get lot of truths and authentic facts than just those things that agents and detectives might discover, compared to the
  • Gravatar canvas shoes May 13th, 2014 at 08:01
    Only wanna input on few general things, The website design is perfect, the written content is very fantastic : D.
  • Gravatar christian louboutin discount site May 27th, 2014 at 18:30
    Enfin, le fifteen mars 1850, la fameuse loi Falloux ¨|tendit ces dispo- sitions aux ¨|coles secondai- res priv¨|es et supprima le monopole accord¨| aux lyc¨|es par Napol¨|on.
  • Gravatar christian louboutin solde June 5th, 2014 at 03:06
    Ils sont totalement incompétent et incorrect.
  • Gravatar jordan 6 noir June 5th, 2014 at 03:07
    Sans que le portrait soit hagiographique, l que porte le metteur en sc son personnage transpire de toutes les sc
  • Gravatar wow gold June 5th, 2014 at 10:23
    We ordered 1/2 more compact since several wanted and need right now I just journeyed 1/2 even bigger. I draw 9 1/2 and may even begin using a 10. I feel preserving wow gold contribute to I am going on a break so they fit in now just simply warm and require help become wow gold shut off! Otherwise just about all I was expecting, really love wow gold!
  • Gravatar wow gold June 6th, 2014 at 17:07
    These can be lovable wow gold. The first couple I got your shines launched getting right off. I resulted in people also receieved a fabulous pair of slip-on's. These might seem like ok. Comfortable. These are undoubtedly individual initially couple of wow gold. I would certainly decide to purchase an alternative duo through a further colours.
  • Gravatar poloralphlaurenuk.a1ajobs June 16th, 2014 at 02:49
    <strong><em><a href=" http://longchampoutletsale.esctify.com/">longchampoutletsale.esctify.com</a></em></strong>
    poloralphlaurenuk.a1ajobs http://poloralphlaurenuk.a1ajobs.com/
  • Gravatar sweat zipp茅 hollister June 27th, 2014 at 05:25
    Gallery Exhibition and the Annual Student Exhibition, the latter through
  • Gravatar nike high heel July 5th, 2014 at 22:43
    Ces battements delaware tambours cérémoniels, décorés aux couleurs rastafaries ainsi que faits à l.a. primary au moyen deb peau signifiant chèvre ou bien delaware vache tendue, combinés à l.a. pureté spirituelle des membres en communauté seront censés évoquer ces pouvoirs protecteurs toutefois d'autant plus offensifs
  • Gravatar mbt shoes July 20th, 2014 at 02:45
    of transporation} deb pens personnelle
  • Gravatar chanel handbags July 21st, 2014 at 08:21
    I enjoy you because of your own labor on this website. My mom really loves conducting internet research and it is easy to understand why. We know all concerning the powerful mode you give efficient tips through this web site and even increase response from website visitors on the idea then our daughter is really understanding a lot of things. Take pleasure in the rest of the new year. You are always conducting a terrific job.
  • Gravatar chanel bags July 24th, 2014 at 06:20
    I'm also writing to make you know what a fine discovery my cousin's child experienced browsing your webblog. She came to find plenty of things, which include what it's like to possess an awesome coaching mood to make other individuals quite simply have an understanding of various multifaceted matters. You truly exceeded people's expectations. Many thanks for providing such beneficial, healthy, informative not to mention fun tips on your topic to Evelyn.
  • Gravatar instagram followers July 24th, 2014 at 11:01
    Very descriptive post, I liked that a lot. Will there be a
    part 2?
Gravatar