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
This article is also available in the following languages:
Carsten Siemens edited Revision 9. Comment: Added tag: has comment
Richard Mueller edited Revision 7. Comment: Replace RGB values with color names in HTML to restore colors
Richard Mueller edited Revision 6. Comment: Removed (en-US) from title, added tags
Luigi Bruno edited Revision 5. Comment: Fixed some links in the "Other Languages" section.
Fernando Lugão Veltem edited Revision 4. Comment: added portuguse version
Horizon_Net edited Revision 3. Comment: added link to german version
Luigi Bruno edited Revision 2. Comment: Added the "Other Languages" section.
Luigi Bruno edited Revision 1. Comment: Edited article's title and tags list.
Ed Price MSFT edited Original. Comment: Updated title casing and cleared formatting on non-code at bottom.
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