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. ( You could perhaps add these to a collection which was stored in App.Current.Resources. )
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: