65.9K
CodeProject is changing. Read more.
Home

Making the ApplicationBar bindable

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.50/5 (2 votes)

May 12, 2011

CPOL
viewsIcon

8760

downloadIcon

80

How to make the ApplicationBar bindable.

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.

<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:

controls:ApplicationBarHelper.BindApplicationBar="True"

And that’s it...

Here is the latest source.