Search code examples
windows-phone-7behaviorapplication-bar

How do I add a behavior to ApplicationBarIconButton in C#?


I am attempting to add items to an application bar with behavoirs.

In xaml they look like:

<phone:PhoneApplicationPage.ApplicationBar>
  <shell:ApplicationBar IsVisible="True"
                          IsMenuEnabled="True">
      <shell:ApplicationBarIconButton x:Name="Save" 
                                      IconUri="/resources/icons/appbar.check.rest.png"
                                      Text="Save"  />
      <shell:ApplicationBarIconButton x:Name="Cancel"
                                      IconUri="/resources/icons/appbar.cancel.rest.png"
                                      Text="Cancel"  />
  </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

<i:Interaction.Behaviors>
  <Behaviors:ApplicationBarIconButtonCommand TextKey="Save" 
                                             CommandBinding="{Binding SaveEventSetupCommand}" />
  <Behaviors:ApplicationBarIconButtonCommand TextKey="Cancel" 
                                             CommandBinding="{Binding CancelEventSetupCommand}" />
</i:Interaction.Behaviors>

For multi language support I need to add something like:

Text="{Binding Path=Localizedresources.lblCourse, Source={StaticResource LocalizedStrings}}"

to each button. It would appear that this cannot be done in xaml, hence the use of code.

The button is added in this code:

ApplicationBarIconButton appBarSaveButton = new ApplicationBarIconButton(
            new Uri("/resources/icons/appbar.check.rest.png", UriKind.Relative)) 
            { Text = "Test" };

ApplicationBar.Buttons.Add(appBarSaveButton);

I just can't figure how to add the behavior. This is my start point:

WP7Contrib.View.Controls.Behaviors.ApplicationBarIconButtonCommand 
            ibc = new WP7Contrib.View.Controls.Behaviors.ApplicationBarIconButtonCommand 
{ TextKey = "Test" };

Basically I am looking for a working sample if anyone can oblige.

Thanks


Solution

  • Based on Matt's answer this is my solution:

    Add this at the top of the xaml page:

    xmlns:Preview="clr-namespace:Phone7.Fx.Preview;assembly=Phone7.Fx.Preview"
    

    and this inside the Grid at the bottom:

        <Preview:BindableApplicationBar x:Name="AppBar" BarOpacity="1.0" IsVisible="{Binding IsBarVisible}" >
            <Preview:BindableApplicationBarIconButton 
                Command="{Binding SaveCommand}"
                Text="{Binding Path=Localizedresources.lblSave, Source={StaticResource LocalizedStrings}}" 
                IconUri="/resources/icons/appbar.check.rest.png" />
            <Preview:BindableApplicationBarIconButton 
                Command="{Binding CancelCommand}"
                Text="{Binding Path=Localizedresources.lblCancel, Source={StaticResource LocalizedStrings}}" 
                IconUri="/resources/icons/appbar.cancel.rest.png" />
        </Preview:BindableApplicationBar>
    

    References:
    http://blog.humann.info/post/2010/08/27/How-to-have-binding-on-the-ApplicationBar.aspx

    http://www.codeproject.com/KB/windows-phone-7/CommandToAppBarWP7.aspx?display=Mobile