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.


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:

InRiver: Not loading your extensions?

(You really need to in the loop to appreciate the issue this post addresses). Man, I've been fighting this problem for hours before I ...