Ouadie
Ouadie

Reputation: 13185

ApplicationBar Bindings Windows Phone

Can't Bind ApplicationBar, I tried:

<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
        <shell:ApplicationBarIconButton 
            x:Name="btnTest"
            IconUri="/Assets/AppBar/appbar.add.rest.png" 
            Text="{Binding MyBtnText}" />
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

and

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    btnTest.Text = AppResources.Reset;
}

Upvotes: 1

Views: 3526

Answers (2)

lisp
lisp

Reputation: 4198

ApplicationBar is not a DependencyObject and doesn't support Bindings. Even when you only want to use bindings for AppBar localization (and not actions or automatic enabling in sync with ViewModel) and end up with moving whole ApplicationBar setup to codebehind, you should consider using a library that gives you greater flexibility with AppBar in xaml. See for example BindableApplicationBar or CaliburnBindableAppBar. There are also other open source project that allow Bindings for AppBar.

sample:

<bar:BindableApplicationBarButton
    Text="{Binding IconButtonText}"
    IconUri="{Binding IconUri, FallbackValue=/Icons/Dark/appbar.add.rest.png}"
    IsEnabled="{Binding ButtonIsEnabled}" />

Upvotes: 2

Ouadie
Ouadie

Reputation: 13185

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    ApplicationBarIconButton btn= ApplicationBar.Buttons[0] as ApplicationBarIconButton;
    if (btn!= null)
    {
        btn.Text = AppResources.Test;
    }

}

Source: http://www.diaryofaninja.com/blog/2011/07/05/solved-why-donrsquot-applicationbar-bindings-work-ndash-windows-phone-7-sdk

or Build the ApplicationBar from code behind C#

// Build a localized ApplicationBar
private void BuildLocalizedApplicationBar()
{
    // Set the page's ApplicationBar to a new instance of ApplicationBar.
    ApplicationBar = new ApplicationBar();

    // Create a new button and set the text value to the localized string from AppResources.
    ApplicationBarIconButton appBarButton = 
        new ApplicationBarIconButton(new   
        Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        appBarButton.Text = AppResources.Reset;
    ApplicationBar.Buttons.Add(appBarButton);

    // Create a new menu item with the localized string from AppResources.
    ApplicationBarMenuItem appBarMenuItem = 
        new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
    ApplicationBar.MenuItems.Add(appBarMenuItem);
}

Source:

How to build a localized app for Windows Phone

Tips for Localizing Windows Phone 8 XAML Apps - Part1

Upvotes: 4

Related Questions