Reputation: 679
Appreciate that the WP7 ApplicationBarIcon is not standard control as such.
I need to be able to hide this programatically (I need to hide rather than disable)
1/ is there any other way I can do this other than adding/removing the icon
2/ assuming that I have to add and remove it, how do I associate an event to the control that I am adding?
Upvotes: 3
Views: 887
Reputation: 65564
The following shows: creating an appbar in code; adding a button to it (including a "click" event handler); and removing the specific button.
this.ApplicationBar = new ApplicationBar();
var newButton = new ApplicationBarIconButton();
newButton.IconUri = new Uri("/images/remove.png", UriKind.Relative);
newButton.Text = "remove";
newButton.Click += RemoveAppBarButton;
this.ApplicationBar.Buttons.Add(newButton);
void RemoveAppBarButton(object sender, EventArgs e)
{
for (var i = 0; i < this.ApplicationBar.Buttons.Count; i++)
{
var button = this.ApplicationBar.Buttons[i] as ApplicationBarIconButton;
if (button != null)
{
if (button.Text == "remove")
{
this.ApplicationBar.Buttons.RemoveAt(i);
break;
}
}
}
}
The important thing to note is that you can't refer to buttons (or menu items) by name.
Upvotes: 4
Reputation: 5377
1) The ApplicationBarIcons do not support some kind of Visibility property. The only thing you can do is to remove and add them. Another solution is to disable them, because this results in a more consistent UI. In the case you show 4 Icons and remove 2 of them, the icons were realigned and the icons are now at positions where other buttons have been. This might confuse the user because he was used to click the second button on the right that now performs a different operation.
2) When I had to deal with that problem, I created a management class holding all icons I needed. When removing an icon, I just removed it from the ApplicationBar but kept it in my class. Later I could add the icon back to the ApplicationBar using exactly the same instance as before with all existing events attached to it.
The ApplicationBar is one thing on Windows Phone 7 that disappoints me compared the the good overall framework.
Hope this helps...
Upvotes: 1