Issue setting Icon on RadContextMenu

6 posts, 0 answers
  1. Ravin
    Ravin avatar
    16 posts
    Member since:
    Jun 2012

    Posted 01 Oct 2015 Link to this post

    Using this example as a base, I created a custom class to add a special right click menu onto any RadGridView item:

    Example: https://github.com/telerik/xaml-sdk/blob/master/GridView/CopyPasteFunctionality/GridViewClipboardContextMenu.cs

    Code:

     

    public GridViewCopyContextMenu(RadGridView grid)
    {
        this.gridView = grid;
     
        Image iconCopy = new Image() {
            Stretch = Stretch.None,
            Source = new BitmapImage(new Uri("/Images/copyIcon.png", UriKind.RelativeOrAbsolute))
        };
     
        this.contextMenu = new RadContextMenu();
        this.contextMenu.Items.Add(new RadMenuItem() { Header = GridViewCopyContextMenu.COPY_SELECTED_CELL_WITHOUT_HEADER_HEADER });
        this.contextMenu.Items.Add(new RadMenuItem() { Header = GridViewCopyContextMenu.COPY_SELECTED_CELL_WITH_HEADER_HEADER });
        this.contextMenu.Items.Add(new RadMenuItem() { Header = GridViewCopyContextMenu.COPY_SELECTED_ROW_WITHOUT_HEADER_HEADER });
        this.contextMenu.Items.Add(new RadMenuItem() { Header = GridViewCopyContextMenu.COPY_SELECTED_ROW_WITH_HEADER_HEADER });
        this.contextMenu.Items.Add(new RadMenuItem() { Header = GridViewCopyContextMenu.COPY_ALL_ROW_WITHOUT_HEADER_HEADER });
        this.contextMenu.Items.Add(new RadMenuItem() { Header = GridViewCopyContextMenu.COPY_ALL_ROW_WITH_HEADER_HEADER });
     
        this.contextMenu.Opened += RadContextMenu_Opened;
        this.contextMenu.ItemClick += RadContextMenu_ItemClick;
     
        RadContextMenu.SetContextMenu(this.gridView, this.contextMenu);
    }

    If I modify any single item to add "Icon = iconCopy, " into the properties, I get the following runtime error:

    Category: ParserError
    Message: Failed to assign to property 'System.Windows.Controls.ContentPresenter.Content'.

    I've tried using "/Images/copyIcon.png" and "MyProject;component/Images/copyIcon.png"; the end result is the same. If I put the source with the either format in xaml on a RadContextMenu, I can see the actual intended image during design time, but I get the same runtime error during runtime.

     

    What can I do to fix this so my icon will show? I am using Silverlight 5, on Runtime Version v4.0.30319, with Telerik Version 2014.1.224.1050.

     

    Thanks
  2. Nasko
    Admin
    Nasko avatar
    588 posts

    Posted 05 Oct 2015 Link to this post

    Hi Ravin,

    Using the provided code-snippet we tried to reproduce the observed by you error but it seems everything works as expected on our side - please, check the attached video and let us know if we didn't miss something.

    We are familiar with that error, however it was only reproducible with SL4. Using SL5 (the version you are using) everything should works as expected. Could you please try to isolate the error in SL5 and send it back to us - thus we could be able to continue our investigation?

    We are looking forward to hearing from you.

    Regards,
    Nasko
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  3. DevCraft banner
  4. Ravin
    Ravin avatar
    16 posts
    Member since:
    Jun 2012

    Posted 05 Oct 2015 Link to this post

    I did confirm that the version is Silverlight 5, the assembly versions you were using were the same, I did see your Image had Width and Height set instead of Stretch, so I changed mine, but I'm still getting the same error.

     

    This is what I found:

    Exception: System.Exception {MS.Internal.WrappedException}

    Inner Exception: Error HRESULT E_FAIL has been returned from a call to a COM component.
       at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
       at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Controls.ScrollContentPresenter.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)

    Which triggers the same error message I was getting before:

    Line: 58
    Error: Unhandled Error in Silverlight Application
    Code: 2531    
    Category: ParserError       
    Message: Failed to assign to property 'System.Windows.Controls.ContentPresenter.Content'.     
    File:      
    Line: 5497     
    Position: 78     

    Thanks,
    Ravin

  5. Nasko
    Admin
    Nasko avatar
    588 posts

    Posted 06 Oct 2015 Link to this post

    Hello Ravin,

    I tried to reproduce the error for SL5 on our side but without any success. I am attaching the sample project that we used for our tests. Could you please, try to modify it in order to reproduce the error and send the project back to us?With a sample project that reproduces the issue we could be able to continue our investigation and provide you with a prompt solution or same detailed technical instructions how to resolve it.

    We are looking forward to hearing from you. 

    Regards,
    Nasko
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  6. Ravin
    Ravin avatar
    16 posts
    Member since:
    Jun 2012

    Posted 13 Oct 2015 Link to this post

    Changing the code in your sample helped me replicate the issue, basically you can't set 2 or more RadMenuItem Icon properties to the same object or you get the error:

    Image iconCopy = new Image()
    {
        Width = 16,
        Height = 16,
        Source = new BitmapImage(new Uri("/RadContextMenuIconSL;component/Images/copy.png", UriKind.RelativeOrAbsolute))
    };
     
    this.GridContextMenu = new RadContextMenu();
    this.GridContextMenu.Items.Add(new RadMenuItem() { Header = "Header1", Icon = iconCopy });
    this.GridContextMenu.Items.Add(new RadMenuItem() { Header = "Header2", Icon = iconCopy });

    Having found the error, I did a bit of research and found I could instead do something like the following to get it to work:

    //Image iconCopy = new Image()
    //{
    //    Width = 16,
    //    Height = 16,
    //    Source = new BitmapImage(new Uri("/RadContextMenuIconSL;component/Images/copy.png", UriKind.RelativeOrAbsolute))
    //};
     
    DataTemplate iconDataTemplate = XamlReader.Load(
            <Image Source=""/RadContextMenuIconSL;component/Images/copy.png"" Width=""16"" Height=""16"" />
        </DataTemplate>") as DataTemplate;
     
    this.GridContextMenu = new RadContextMenu();
    this.GridContextMenu.Items.Add(new RadMenuItem() { Header = "Header1", IconTemplate = iconDataTemplate });
    this.GridContextMenu.Items.Add(new RadMenuItem() { Header = "Header2", IconTemplate = iconDataTemplate });

    Thanks,
    Ravin

  7. Nasko
    Admin
    Nasko avatar
    588 posts

    Posted 14 Oct 2015 Link to this post

    Hello Ravin,

    I am really glad that you managed to find the reason for your issue and we are really thankful for sharing with us your solution.

    If you have any additional questions or concerns please, do not hesitate to contact us.

    Regards,
    Nasko
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
Back to Top
DevCraft banner