Click here to Skip to main content
15,884,838 members
Articles / Mobile Apps / Windows Phone 7

Making the ApplicationBar bindable

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
14 Mar 2011CPOL 7.6K   47   4  
How to make the ApplicationBar bindable in a WP7 application.

Capture.png

One of the small issues with using MVVM (Model-View-ViewModel) in a WP7 application is that both ApplicationBarIconButton and ApplicationBarMenuItem do not derive from DependencyObject. This means that I can not bind to any of its properties, making a “true” MVVM application a little hard! I created two simple wrappers for these that make it possible to “bind” to a method.

XML
<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
        <controls:MethodApplicationBarIconButton 
           IconUri="/Images/appbar_button1.png" 
           Text="Button 1" MethodName="ShowMessage" />
        <controls:MethodApplicationBarIconButton 
           IconUri="/Images/appbar_button2.png" 
           Text="Button 2" MethodName="ShowMessage" />
        <shell:ApplicationBar.MenuItems>
            <controls:MethodApplicationBarMenuItem 
              Text="MenuItem 1" MethodName="ShowMessage" />
            <controls:MethodApplicationBarMenuItem 
              Text="MenuItem 2" MethodName="ShowMessage" />
        </shell:ApplicationBar.MenuItems>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

The only “little” caveat is that you have to set the BindApplicationBar attached property to true:

XML
controls:ApplicationBarHelper.BindApplicationBar="True"

And that’s it...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --