WP7 ApplicationBarButton Bug

Topics: Bugs
Oct 19, 2011 at 4:19 PM

Windows Phone does not set the binding of the active textbox if an application bat button (or menu, not testet) is used. This results in the method being called prior to setting the property bound to the textbox

i.e.:
- Clicking a Textbox bound to a string propery
- Entering Text without leaving the textbox
- Clicking an application bar button
- The method of the button is executed
- the property bound to the textbox is updated

I implemented a bugfix directy in the click handler of the caliburn AppBarButton / Menu. Maybe you could implement the changes in future releases or someone else can use it :-)

    class AppBarButtonTrigger : TriggerBase<PhoneApplicationPage> {
        public AppBarButtonTrigger(IApplicationBarMenuItem button) {
            button.Click += ButtonClicked;
        }

        void ButtonClicked(object sender, EventArgs e) {
            // Update binding of active TextBox to update the binding prior to the method call
            object focusObj = FocusManager.GetFocusedElement();
            if (focusObj != null && focusObj is TextBox)
            {
                var binding = (focusObj as TextBox).GetBindingExpression(TextBox.TextProperty);
                binding.UpdateSource();
            }

            InvokeActions(e);
        }
    }

    class AppBarMenuItemTrigger : TriggerBase<PhoneApplicationPage> {
        public AppBarMenuItemTrigger(IApplicationBarMenuItem menuItem) {
            menuItem.Click += ButtonClicked;
        }

        void ButtonClicked(object sender, EventArgs e) {
            // Update binding of active TextBox to update the binding prior to the method call
            object focusObj = FocusManager.GetFocusedElement();
            if (focusObj != null && focusObj is TextBox)
            {
                var binding = (focusObj as TextBox).GetBindingExpression(TextBox.TextProperty);
                binding.UpdateSource();
            }

            InvokeActions(e);
        }
    }

Oct 24, 2011 at 3:56 AM

how are you binding your text boxes, because cm does actually have the binding source update for textboxes for silverlight based apps, if you actually take advantage of the Conventions already built in.