AppBarButton IsEnabled VM binding

Oct 17, 2010 at 9:04 PM

I'm running into one little glitch with the new AppBarButton (which is a great missing piece in the puzzle - thanks, Rob). I can't seem to bind the IsEnabled property to a ViewModel property to turn these on and off depending on state. If, for example, I use IsEnabled="{Binding IsThisButtonEnabled}" and the VM property is a bool, I get the dreaded AG_E_PARSER_BAD_PROPERTY_VALUE error on that line. If I just set them "True" or "False" the IsEnabled property parses just fine, but then I have no control over it. I tried a ValueConverter returning "True" or "False", but this also fails.

Coordinator
Oct 17, 2010 at 9:51 PM

Use the Can* property/method Action convention on your view model. That will make it work. The underlying WP7 platform does not allow databinding on IsEnabled.  I managed to find a way to make it work with actions...

Oct 18, 2010 at 1:21 AM

That was a DOH moment for me for sure - minutes after I posted it, I realized it would be a Can property. Nice job tricking the phone :).

Oct 18, 2010 at 3:03 AM
Edited Oct 18, 2010 at 9:14 AM

OK. There's still a little bit of a disconnect with Localization and Theming. If I declare a cal:AppBarButton in XAML, I get to use the Message property and commanding works. But then I cannot bind the IconUri and Text to VM properties that return the properly themed icon (Light or Dark) and localized text for the button. That is, I must declare them in XAML as hard-coded strings, which are invalid if the theme is not Dark, or the CultureInfo is not en-US.

If I try to access these properties by x:Name of the AppBarButton in the code-behind, knowing what the theme is and using the same localization class, I can see the Button in Intellisense, but it is null in a Page_Loaded event (I know the VM is wired up at this point), and I get an exception. If I build the AppBar in Code-behind, the Message property doesn't get wired, and commanding does not work.

I've been able to get my app to work with localization and swapped-out icon Uri's using code-behind build of the ApplicationBar and click events for actions, but would really prefer to move all of that code to theVM. This is the last piece of the puzzle.

signed,

behind a rock and a hard place :(

P.S. I have a workaround and it appears to be centered around the timing of code-behind access. In code-behind right after InitializeComponent(), I now call a method to swap out icons based on the current theme if necessary. I then build the ApplicationBar in code using AppBarButtons, and the localization, icon and Message action all stick to the wall, as it were.