Updating ApplicationBarIconButton Dynamically

Updating ApplicationBarIconButton Dynamically

 

Since the ApplicationBar is not a DependencyObject and not part of the visual tree, FindElement cannot locate Items in the ApplicationBar with x:Name set.  The members will be null.

 

Due to this, it may be best to initialize the ApplicationBar using code behind and hold references to items which need to be dynamically changed.

 

Notice in the code below that SaveEdit reference is held and SaveEdit.IsEnabled is updated with each keypress into the TextBox.

 

#region AppBar

private ApplicationBarIconButton SaveEdit;

private void InitAppBar()

{

ApplicationBar appBar = new ApplicationBar();

appBar = new ApplicationBar();

 

SaveEdit = new ApplicationBarIconButton(new Uri("images/appbar.check.rest.png", UriKind.Relative));

SaveEdit.Click += new EventHandler(OnClick_Check);

//SaveEdit.Text = Strings.Save_button;

appBar.Buttons.Add(SaveEdit);

 

ApplicationBarIconButton CancelEdit = new ApplicationBarIconButton(new Uri("images/appbar.close.rest.png", UriKind.Relative));

CancelEdit.Click += new EventHandler(OnClick_Cancel);

//CancelEdit.Text = Strings.Cancel_button;

appBar.Buttons.Add(CancelEdit);

 

ApplicationBar = appBar;

}

#endregion AppBar

       

 

 

void itemName_KeyDown(object sender, KeyEventArgs e)

{

bool isNameValid = itemName.Text.Length != 0;

           

if ((e.Key == Key.Enter) && isNameValid)

{

SaveAndExit();

}

           

SaveEdit.IsEnabled = isNameValid;

}

 

Original article source: http://myfun.spaces.live.com/blog/cns!AC1291870308F748!531.entry

 




Other Languages

This article is also available in the following languages:

Italian (it-IT)

Deutsch (de-DE)

Brazilian Portuguese (pt-BR)

See Also

Another important place to find a huge amount of Windows Phone related articles is the TechNet Wiki itself. The best entry point is Windows Phone Resources on the TechNet Wiki.
Sort by: Published Date | Most Recent | Most Useful
Comments
  • I recommend the library shwp.codeplex.com/.../304079 for working with AppBar.

  • This article was highlighted in the Top Contributors Awards - blogs.technet.com/.../top-contributors-awards-amdt-microsoft-management-packs-lotus-notes-windows-phone-tips-and-the-most-awesome-e-book-gallery-in-the-world.aspx

  • Great Work !

  • This is one of the way you can show your app bar dynamically. But the best way i feel is to put it in page resources with a put a key and whenever the appbar is required you can fetch it from the resources. and set it to the application bar.

    This will make the execution faster i guess.

    ApplicationBar = (ApplicationBar)Resources["SampleApplicationBar"];

         <shell:ApplicationBar x:Key="SampleApplicationBar"

                                 IsVisible="True">

                 </shell:ApplicationBarIconButton>

               <shell:ApplicationBarIconButton  x:Name="AppBarSampleButton"

                                                Text="Sample"

                                                IconUri="Sample.png"

                                                Click="SampleButton_Click" />

           </shell:ApplicationBar>

Page 1 of 1 (4 items)