Saturday 17 January 2009

Investigating Silverlight Exception: System.ArgumentException: Value does not fall within the expected range

Hi,

Good to be back in action after a long break. I just found this nasty little exception when i was adding a user control to a Grid in Silverlight 2. Took me some time to understand what was happening though, until i found that it was a known issue on the silverlight forum.

I managed to get a work around that problem which i promptly shared on the same forum.

System.ArgumentException: Value does not fall within the expected range at Ms.InternalXcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData)

Turns out that every container needs a unique 'Name' property to be set for it's child. I have managed to get it fixed with the following self explanatory code snippet.

private static int componentID = 0;

private Grid tableGrid;

FrameworkElement element; //this is a user control.

element.SetValue(Canvas.NameProperty, GetComponentID().ToString());

private static int GetComponentID()

{

return componentID++;
}

//This will not give the 'Value does not fall within the expected range' exception.

tableGrid.Children.Add(element);

No comments:

Post a Comment