ArgumentException ("Value does not fall within the expected range") when adding items programatically

2 posts, 0 answers
  1. Domagoj
    Domagoj avatar
    17 posts
    Member since:
    Jun 2011

    Posted 21 Jul 2011 Link to this post

    I am trying to fill the TreeView programatically.

    I have two different tree node types so I have derived them from RadTreeViewItem:

    public class ParametersTemplateGroupRTVI : RadTreeViewItem
    {
        public ParametersTemplateGroupRTVI()
        {
            DefaultImageSrc = ExpandedImageSrc = SelectedImageSrc = "/MyProject;component/Images/templateGroup.png";
        }
     
        public ParametersTemplateGroup ParametersTemplateGroup { get; set; }
    }
     
    public class ParametersTemplateRTVI : RadTreeViewItem
    {
        public ParametersTemplateRTVI()
        {
            DefaultImageSrc = ExpandedImageSrc = SelectedImageSrc = "/MyProject;component/Images/template.png";
        }
     
        public ParametersTemplate ParametersTemplate { get; set; }
    }

    ParametersTemplate and ParametersTemplateGroup are domain specific objects (each ParametersTemplateGroup can contain a number of templates (1:N) and any number of other ParametersTemplateGroups (1:N)). Just like a Folder in Windows can contain N files and other folders.

    I have a list of nodes (which can be of one of two types mentioned above), and two dictionaries that contain parametersTemplates and parametersTemplateGroups, accessed by ID.

    private readonly List<RadTreeViewItem> treeNodes = new List<RadTreeViewItem>();
    private readonly Dictionary<int, ParametersTemplateRTVI> templates = new Dictionary<int, ParametersTemplateRTVI>();
    private readonly Dictionary<int, ParametersTemplateGroupRTVI> groups = new Dictionary<int, ParametersTemplateGroupRTVI>();


    From the two dictionaries and the list of all nodes I am trying to connect them the way they are connected (according to the parent property):

    public void ConnectTreeNodes()
    {
        foreach (RadTreeViewItem node in treeNodes)
        {
            ParametersTemplateRTVI templateNode = node as ParametersTemplateRTVI;
            ParametersTemplateGroupRTVI templateGroupNode = node as ParametersTemplateGroupRTVI;
     
            if (templateNode != null)
            {
                ParametersTemplateGroupRTVI parentNode = groups[templateNode.ParametersTemplate.ParametersTemplateGroupID];
                parentNode.Items.Add(node); // ***
            }
            else if (templateGroupNode != null && templateGroupNode.ParametersTemplateGroup.ParentID.HasValue)
            {
                ParametersTemplateRTVI parentNode = templates[templateGroupNode.ParametersTemplateGroup.ParentID.Value];
                parentNode.Items.Add(node);
            }
            else { }
        }
    }

    The line with a commend and three asterisks is the line where I get an ArgumentException thrown. "Value does not fall within the expected range.". This happens when the second node is added, not on the first one. I've even tried (as suggested on another threads) to place something like this before the line that throws an exception:

    node.Name = Guid.NewGuid().ToString();

    But this didn't solve the problem.

    The stack trace at the moment of an exception:

    at MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData)
    at MS.Internal.XcpImports.MethodPack(IntPtr objectPtr, String methodName, Object[] rawData)
    at MS.Internal.XcpImports.Collection_Add[T](PresentationFrameworkCollection`1 collection, Object value)
    at System.Windows.PresentationFrameworkCollection`1.AddImpl(Object value)
    at System.Windows.Controls.ItemCollection.AddImpl(Object value)
    at System.Windows.Controls.ItemCollection.AddInternal(Object value)
    at System.Windows.PresentationFrameworkCollection`1.Add(T value)
    at MyProject.Models.Domain.Templates.ParameterTemplateEditorTree.ConnectTreeNodes()
    at MyProject.Views.TemplateEditor.ParameterTemplateEditorPage.onParametersTemplateLoaded(LoadOperation`1 operation)
    at System.ServiceModel.DomainServices.Client.DomainContext.<>c__DisplayClass13`1.<Load>b__11(LoadOperation lo)
    at System.ServiceModel.DomainServices.Client.LoadOperation.<>c__DisplayClass4`1.<Create>b__0(LoadOperation`1 arg)
    at System.ServiceModel.DomainServices.Client.LoadOperation`1.InvokeCompleteAction()
    at System.ServiceModel.DomainServices.Client.OperationBase.Complete(Object result)
    at System.ServiceModel.DomainServices.Client.LoadOperation.Complete(DomainClientResult result)
    at System.ServiceModel.DomainServices.Client.DomainContext.CompleteLoad(IAsyncResult asyncResult)
    at System.ServiceModel.DomainServices.Client.DomainContext.<>c__DisplayClass1b.<Load>b__17(Object )


    I really don't know how to continue. I don't add that node to any other control at any other place...
    What does this exception mean in this context and how to solve this problem?

    Thanks in advance.
  2. Domagoj
    Domagoj avatar
    17 posts
    Member since:
    Jun 2011

    Posted 22 Jul 2011 Link to this post

    I have found the error and it does not relate directly to Telerik products, it was a bug in my code.

    The else-if should search from the groups dictionary, not the templates. This bug was indeed trying to add the same element to the visual tree.

    else if (templateGroupNode != null && templateGroupNode.ParametersTemplateGroup.ParentID.HasValue)
    {
        ParametersTemplateGroupRTVI parentNode = groups[templateGroupNode.ParametersTemplateGroup.ParentID.Value];
        parentNode.Items.Add(node);
    }

    When I changed it, it worked as intended.

    (Since this is not a bug or a problem related to a Telerik control, moderators should feel free to remove the thread from the forum).
  3. DevCraft banner
Back to Top