This is a migrated thread and some comments may be shown as answers.

Error when clear viewmodel's subitems

7 Answers 104 Views
Menu
This is a migrated thread and some comments may be shown as answers.
bohebolo
Top achievements
Rank 1
bohebolo asked on 15 Sep 2010, 09:01 AM
Here is my container bindings
<telerik:ContainerBindingCollection x:Key="mainMenuContainerBindingCollection">
    <telerik:ContainerBinding PropertyName="Icon" Binding="{Binding Icon}" />
    <telerik:ContainerBinding PropertyName="Command" Binding="{Binding DataContext.MenuOpenCommand, ElementName=layoutRoot}" />
    <telerik:ContainerBinding PropertyName="CommandParameter" Binding="{Binding}" />
</telerik:ContainerBindingCollection>

And my HierarchicalDataTemplate
<telerik:HierarchicalDataTemplate x:Key="mainMenuTemplate" ItemsSource="{Binding SubItems}" telerik:ContainerBinding.ContainerBindings="{StaticResource mainMenuContainerBindingCollection}">
    <TextBlock Text="{Binding Text}" />
</telerik:HierarchicalDataTemplate>

Each time i call SubItems.clear(), it will throw an exception:
Unhandled Error in Silverlight Application Failed to assign to property 'System.Windows.Controls.ContentPresenter.Content'.

This will not happen, if my menu doesn't contains any icon. Maybe related to this silverlight's bug ?
Because from my investigation, this exception related to Icon property which also uses ContentPresenter & TemplateBinding.

Currently running:
Silverlight v4.0.50826.0
Telerik.Windows.Controls.Navigation v2010.1.603.1040
Telerik.Windows.Themes.Windows7 v2010.1.603.1040

7 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 16 Sep 2010, 01:13 PM
Hello bohebolo,

If any of Icon or Header is UIElement that then changing the templates will throw this exception. This is caused by bug in the Silverlight Framework (TemplateBindings are not cleared on Template change). When you call menuItem.Items.Clear() all subItems are removed and template is changed.
In order to avoid this issue you have to use HeaderTemplate and IconTemplate. In the IconTemplate place a DataTemplate with Image control and bind it to Icon property of the data item.
The same approach could be applied for HeaderTemplate.

Let us know if you need more information.

Best wishes,
Hristo
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
bohebolo
Top achievements
Rank 1
answered on 17 Sep 2010, 09:55 AM
Hi Hristo ,

Thanks for your response, can you point me out how to achieve "you have to use HeaderTemplate and IconTemplate" ?
Do you mean to assign my own template to SubmenuHeaderTemplateKey/SubmenuItemTemplateKey ?
And in that template, should I use other element (image maybe?), instead of ContentPresenter ?
FYI, i tried to return image type from  my viewmodel's icon property, and it still doesn't work.

Btw, if I do use custom template, then I assume i can't use telerik default themes for my page's RadMenu?
And if my app uses 2 themes (ie. vista & seven), then I should define 2 custom template ?

thanks.

0
Hristo
Telerik team
answered on 20 Sep 2010, 12:12 PM
Hello bohebolo,

You can find the attached project that demonstrate how to set IconTemplate property. In the data item there is ImageUrl property of type string that is bound to Image.Source property. This way when RadMenuItem Template is changed (because you clear all subItems) the content presenter that shows the Icon will not throw an exception.

Let me know if you need more information.

Kind regards,
Hristo
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
bohebolo
Top achievements
Rank 1
answered on 20 Sep 2010, 06:00 PM
Hristo, thanks a lot for your sample project.
It works fine, however when I apply the technique to my app, got this exception.
Message: Unhandled Error in Silverlight Application The property 'IconTemplate' was not found in type 'Telerik.Windows.Controls.RadMenuItem'. [Line: 33 Position: 30]   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)

I think this is a version issue..

0
Accepted
Hristo
Telerik team
answered on 21 Sep 2010, 07:05 AM
Hello bohebolo,

In the attached project I'm using 2010 Q2 SP1. (this is the version specified in the Ticket Info).
If you are using older version please upgrade to the latest internal build (which also contains memory leak fixes).

Regards,
Hristo
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Rob
Top achievements
Rank 1
answered on 20 Oct 2010, 04:49 PM
Hello,

I seem to be experiencing the same problem in my project.  Is there a sequence that i can follow that would allow me to clear the items programmatically without getting this error?  I'm kind of stuck with hardcoded items and can't really use binding without making some other larger changes...

Thanks,
Rob
0
bohebolo
Top achievements
Rank 1
answered on 21 Oct 2010, 04:20 AM
Hi Rob,

You can try Hristo's sample project above.
It really works, but you need to uses Q2 2010 version.
Tags
Menu
Asked by
bohebolo
Top achievements
Rank 1
Answers by
Hristo
Telerik team
bohebolo
Top achievements
Rank 1
Rob
Top achievements
Rank 1
Share this question
or