Error when clear viewmodel's subitems

8 posts, 1 answers
  1. bohebolo
    bohebolo avatar
    31 posts
    Member since:
    Aug 2010

    Posted 15 Sep 2010 Link to this post

    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
  2. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 16 Sep 2010 Link to this post

    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
  3. DevCraft banner
  4. bohebolo
    bohebolo avatar
    31 posts
    Member since:
    Aug 2010

    Posted 17 Sep 2010 Link to this post

    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.

  5. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 20 Sep 2010 Link to this post

    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
  6. bohebolo
    bohebolo avatar
    31 posts
    Member since:
    Aug 2010

    Posted 20 Sep 2010 Link to this post

    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..

  7. Answer
    Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 21 Sep 2010 Link to this post

    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
  8. Rob
    Rob avatar
    238 posts
    Member since:
    Jan 2009

    Posted 20 Oct 2010 Link to this post

    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
  9. bohebolo
    bohebolo avatar
    31 posts
    Member since:
    Aug 2010

    Posted 20 Oct 2010 Link to this post

    Hi Rob,

    You can try Hristo's sample project above.
    It really works, but you need to uses Q2 2010 version.
Back to Top
DevCraft banner