20 December, 2010

WP7: How to programmatically modify menubar buttons

An unexpected caveat is found when attempting to modify the menubar buttons (enable/disable) on a WP7 application.

You can not (as you rightfully would expect) just reference the menubar buttons by name (x:name), and then access this in code. Due to reasons unknown to me, the menubar buttons are only accessible by index.

image

Due to this reason – you access (and manipulate) the button via ApplicationBar.Button[index] instead. In the below code sample, the ApplicationBarIconButton is retrieved via index and referenced as ‘b'.

void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var b = (ApplicationBarIconButton)ApplicationBar.Buttons[1]; //indx based!
b.IsEnabled = (pivItms.SelectedIndex == 1);
}

Technorati Tags:

No comments: