Hi,
How may I set the icon of a XamWebMenuItem programatically? I'm trying to do it but each time I got an erro when I try to open the menu :S
Cheers.
Hi
You can set icon programatically as shown follow:
public MainPage(){ InitializeComponent(); Loaded += new RoutedEventHandler(MainPage_Loaded);}
void MainPage_Loaded(object sender, RoutedEventArgs e){ // Create the image element. Image simpleImage = new Image(); // Create source. BitmapImage bi = new BitmapImage(); bi.UriSource = new Uri("icon_Welcome.png", UriKind.RelativeOrAbsolute);
// Set the image source. simpleImage.Source = bi;
((XamWebMenuItem)this.MyMenu.Items[0]).Icon = simpleImage;}
You should add your icon as a resource to your project.
Todor
It works in the visible XamWebMenuItem, but when i set the same picture with the same code in the subitems, when I try to open the menu appears:
Error: Unhandled Error in Silverlight Application Code: 1001 Category: ParserError Message: AG_E_UNKNOWN_ERROR File: Line: 86 Position: 169
I see... Thanks.
It is a silverlight limitation. Like in WPF you the element can reside only once in logical and visual trees.
You can make a test for that. Create two ContentControl in xaml and try to set same image to them. You will get the above error.
You should create new Image. The image can be child of only one element. Otherwise will get this exception: "Element is already the child of another element."
void MainPage_Loaded(object sender, RoutedEventArgs e){ // Create the image element. Image simpleImage = new Image(); Image simpleImage1 = new Image(); // Create source. BitmapImage bi = new BitmapImage(); bi.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(bi_ImageFailed);
bi.UriSource = new Uri("icon_Welcome.png", UriKind.RelativeOrAbsolute);
// Set the image source. simpleImage.Source = bi; simpleImage1.Source = bi;
((XamWebMenuItem)this.Menu1.Items[0]).Icon = simpleImage; Item1_1.Icon = simpleImage1;}
Ok, I was setting the same Image object to all the menu items, so that was the problem.
Why cannot I reuse the same Image object for all the items?