System.ArgumentException: Value does not fall within the expected range - very poor error handling
-
Thursday, October 21, 2010 11:06 AM
I am currently building a Silverlight online store from scratch, and naturally one of the pages of the store displays the products for a particular category. Each product is displayed by a product UserControl who's display properties are bound to dependancy properties in the code behind. These product UserControls are then loaded inside a WrapPanel to form the product listing.
However rather randomly it would seem, when the process tried to add the product UserControl to the WrapPanel it would fail and throw a less than helpful exception
System.ArgumentException was unhandled by user code Message=Value does not fall within the expected range.
Wow, thanks, that's just so incredibly helpful. I know exactly what the problem is and I'm sure I won't waste an entire day bug-hunting!
Here's the code before I go any further ...
<UserControl x:Name="userControl" x:Class="KiteSurfSup.Products.Controls.KiteboardListingItem" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="264" d:DesignWidth="300"> <Grid x:Name="LayoutRoot" Background="White"> <Rectangle x:Name="BlackBackground" Fill="#FF1D1D1D" Height="91.307" Stroke="Black" VerticalAlignment="Bottom"/> <Path x:Name="GreyTab" Data="F1M1.25,1.75C1.25,1.75 26,1.75 26,1.75 26,1.75 26,58 26,58 26,58 1.25,58 1.25,58 1.25,58 1.25,1.75 1.25,1.75z" Fill="#FFBFBFBF" Height="91.307" Width="48.417" HorizontalAlignment="Left" VerticalAlignment="Bottom" Stretch="Fill"/> <TextBlock x:Name="txtBlckSizes" FontSize="11" LineStackingStrategy="BlockLineHeight" LineHeight="11" TextWrapping="Wrap" Height="12.672" VerticalAlignment="Bottom" Margin="59,0,0,18.555" Text="{Binding StyleItemSizes, ElementName=userControl}"/> <TextBlock x:Name="txtBlckName" Foreground="#FFFAFAFA" FontWeight="Bold" FontSize="14.667" FontFamily="Arial" LineStackingStrategy="BlockLineHeight" LineHeight="14" TextAlignment="Left" TextWrapping="Wrap" Text="{Binding StyleName, ElementName=userControl}" Height="14" Margin="59,0,8,68.627" VerticalAlignment="Bottom"/> <TextBlock x:Name="txtBlckDescription" Foreground="#FFE3E3E3" FontSize="12" FontFamily="Arial" LineStackingStrategy="BlockLineHeight" LineHeight="9" TextAlignment="Left" TextWrapping="Wrap" Text="{Binding StyleDescription, ElementName=userControl}" Height="29.4" Margin="59,0,8,35.227" VerticalAlignment="Bottom"/> <Image x:Name="imgImage" Source="{Binding StyleImage, ElementName=userControl}" HorizontalAlignment="Center" Height="244.68" VerticalAlignment="Top"/> <HyperlinkButton x:Name="hyprLnkBtnGoToDetails" Height="13" Content="details »" FontSize="9" Foreground="#FF0587A7" Margin="59,0,-9,1.555" VerticalAlignment="Bottom" FontFamily="Arial"/> <Image Height="44.454" Margin="46.285,0,0,45.231" Source="Image2.png" Stretch="Fill" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="87.984"> <Image.RenderTransform> <CompositeTransform Rotation="90"/> </Image.RenderTransform> </Image> </Grid> </UserControl>using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Media.Imaging; namespace KiteSurfSup.Products.Controls { public partial class KiteboardListingItem : UserControl { public int ProductId { get { return (int)GetValue(ProductIdProperty); } set { SetValue(ProductIdProperty, value); } } // Using a DependencyProperty as the backing store for ProductId. This enables animation, styling, binding, etc... public static readonly DependencyProperty ProductIdProperty = DependencyProperty.Register("ProductId", typeof(int), typeof(KiteboardListingItem), new PropertyMetadata(0)); public string StyleName { get { return (string)GetValue(StyleNameProperty); } set { SetValue(StyleNameProperty, value); } } // Using a DependencyProperty as the backing store for StyleName. This enables animation, styling, binding, etc... public static readonly DependencyProperty StyleNameProperty = DependencyProperty.Register("StyleName", typeof(string), typeof(KiteboardListingItem), new PropertyMetadata("Item Name")); public string StyleDescription { get { return (string)GetValue(StyleDescriptionProperty); } set { SetValue(StyleDescriptionProperty, value); } } // Using a DependencyProperty as the backing store for ItemDescription. This enables animation, styling, binding, etc... public static readonly DependencyProperty StyleDescriptionProperty = DependencyProperty.Register("StyleDescription", typeof(string), typeof(KiteboardListingItem), new PropertyMetadata("")); public BitmapImage StyleImage { get { return (BitmapImage)GetValue(StyleImageProperty); } set { SetValue(StyleImageProperty, value); } } // Using a DependencyProperty as the backing store for ItemImage. This enables animation, styling, binding, etc... public static readonly DependencyProperty StyleImageProperty = DependencyProperty.Register("StyleImage", typeof(BitmapImage), typeof(KiteboardListingItem), new PropertyMetadata(null)); public string StyleItemSizes { get { return (string)GetValue(StyleItemSizesProperty); } set { SetValue(StyleItemSizesProperty, value); } } // Using a DependencyProperty as the backing store for ItemSizes. This enables animation, styling, binding, etc... public static readonly DependencyProperty StyleItemSizesProperty = DependencyProperty.Register("StyleItemSizes", typeof(string), typeof(KiteboardListingItem), new PropertyMetadata("")); public KiteboardListingItem() { InitializeComponent(); } } }
The code with the WrapPanel that loads the product UserControls<navigation:Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:localControl="clr-namespace:KiteSurfSup.Products.Controls" xmlns:sharedLibs="clr-namespace:KiteSurfSup.Shared.Libraries;assembly=KiteSurfSup.Shared" xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" xmlns:controls="clr-namespace:KiteSurfSup.Shared.Controls;assembly=KiteSurfSup.Shared" xmlns:localControls="clr-namespace:KiteSurfSup.Products.Controls" xmlns:shared="clr-namespace:KiteSurfSup.Shared.Controls;assembly=KiteSurfSup.Shared" xmlns:KiteSurfSup_Products="clr-namespace:KiteSurfSup.Products" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" x:Class="KiteSurfSup.Products.Views.Kiteboards" d:DesignWidth="1024" d:DesignHeight="654" mc:Ignorable="d" > <Grid x:Name="LayoutRoot"> <Canvas x:Name="Products2" Clip="M0,0L1024,0 1024,762 0,762z" HorizontalAlignment="Left" Height="762" UseLayoutRounding="False" VerticalAlignment="Top" Width="1024"> <Canvas x:Name="Flattened" Height="654" Canvas.Left="22" Canvas.Top="0" Width="980"> <Image x:Name="Layer_48" Height="654" Canvas.Left="0" Source="Products2_Images/Layer 48.png" Canvas.Top="0" Width="980"/> <shared:FrontEndLeftNavAndFooter Height="652" Canvas.Left="2" Canvas.Top="2" Width="977"/> </Canvas> <ScrollViewer x:Name="scrllVwrProducts" BorderBrush="Transparent" Background="Transparent" Height="555" Width="666" VerticalAlignment="Top" Canvas.Left="314" Canvas.Top="57"> <toolkit:WrapPanel x:Name="wrpPnlProducts" Width="656"/> </ScrollViewer> </Canvas> </Grid> </navigation:Page>
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Navigation; using System.ServiceModel.DomainServices.Client; using KiteSurfSup.RIA.Web; using KiteSurfSup.Shared.Libraries; using KiteSurfSup.Shared; using KiteSurfSup.Shared.Dialogues; namespace KiteSurfSup.Products.Views { public partial class Kiteboards : Page { private KiteSurfSupDomainContext Context = new KiteSurfSupDomainContext(); public int CurrentDepartmentId = 2; // kiteboards public Kiteboards() { InitializeComponent(); this.Loaded += new RoutedEventHandler(Kiteboards_Loaded); } void Kiteboards_Loaded(object sender, RoutedEventArgs e) { EntityQuery<KiteSurfSup.RIA.Web.ProductsAndPropertiesByDepartment_Result> styles = this.Context.GetProductsAndPropertiesByDepartmentQuery(CurrentDepartmentId, (int)Config.SkuIndex.Style); //this.ModalBusyIndicator.IsBusy = true; this.Context.Load(styles, (callpackParam) => { if (!callpackParam.HasError) { int index = 0; int lastStyleId = 0; Controls.KiteboardListingItem currentItem = new Controls.KiteboardListingItem(); foreach (ProductsAndPropertiesByDepartment_Result item in callpackParam.Entities) { // if this is a new item if (lastStyleId != item.StyleId) { // if this isnt the first time we're adding an item (need to populate it properties before adding it) if (index > 1) { // add the object to the container currentItem.Margin = new Thickness(10); (this.scrllVwrProducts.FindName("wrpPnlProducts") as WrapPanel).Children.Add(currentItem); } // create a new listing item currentItem = new Controls.KiteboardListingItem(); //currentItem.ProductId = item.ProductId; //currentItem.StyleName = item.StyleName; lastStyleId = item.StyleId ?? 1; index++; } } } else { //this.ModalBusyIndicator.IsBusy = false; ErrorWindow.CreateNew(callpackParam.Error); } }, null); } // Executes when the user navigates to this page. protected override void OnNavigatedTo(NavigationEventArgs e) { } } }
Every second, third or fourth time (seems pretty random) the product UserControls were added to the WrapPanel on this line(this.scrllVwrProducts.FindName("wrpPnlProducts") as WrapPanel).Children.Add(currentItem);I get the expection
System.ArgumentException was unhandled by user code Message=Value does not fall within the expected range. StackTrace: at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.Collection_AddValue[T](PresentationFrameworkCollection`1 collection, CValue value) at MS.Internal.XcpImports.Collection_AddDependencyObject[T](PresentationFrameworkCollection`1 collection, DependencyObject value) at System.Windows.PresentationFrameworkCollection`1.AddDependencyObject(DependencyObject value) at System.Windows.Controls.UIElementCollection.AddInternal(UIElement value) at System.Windows.PresentationFrameworkCollection`1.Add(T value) at KiteSurfSup.Products.Views.Kiteboards.<Kiteboards_Loaded>b__0(LoadOperation`1 callpackParam) 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 ) InnerException:Right then - great, fantastically USELESS error message that I'm becoming accustomed to working with WPF and WCF. Like get it together - probably the biggest thorn in my side with .NET 3.0+ is the hours wasted hunting down exceptions like "An error occurred” … anyway, rant over
Now the way I originally set the product Usercontrol was by using Expression Blend using the New Bindings Window. It turns out this is what’s causing the problem
If you notice at the top of the UserControl Blend added a x:name property
<UserControl x:Name="userControl"
This is what's really freaking it out. Removing this property removes the exception.I'll post my workaround when I find one.
** Please excuse untidy code - the bug hunt has meant hacking it
All Replies
-
Monday, October 25, 2010 9:51 PM
YOU ARE AWESOME!
Thanks heaps for the going through the pain of identifying that for us.Cheers man.
-
Tuesday, November 16, 2010 8:53 PM
I have also lost a few Hr's over this BEEP in problem, Now I know why the Mother Board is named that we as with this issue, I have said many 4 letter words while trying to crack this. This post has really saved my ass, Thanks poster you are a genius,
why don't we call Mother Xaml with the 4 letter word removed.
Don't get me wrong I used to love Javascript but sine SL has come out I am now a SL fanatic, a converted MS geek.
but when SL goes wrong some *** becomes hard to crack, thanks again to this web page may others find it when they need it. -
Thursday, July 21, 2011 6:28 AM
Thanks a ton!
It helped me a lot to identify the issue with my code. I would always look for the changes blend makes to my xaml - intended on unintended changes!
/Digvijay
-
Saturday, October 08, 2011 9:07 AM
We are having the same problem -- adding UserControls to a WrapPanel occassionally causes the exception.
Unfortunately, our UserControls use VisualStates, so they have to have a name. I wonder if the fact that all the different UserControls have the same name (even though they are different classes) is causing the mischief? If so, why is it intermittent?
As you point out, the exception message is spectaculerly useless.
-
Saturday, October 08, 2011 8:28 PM
The problem is intermittent probably because raises an exception only when it finds a duplicate in the visual tree. The usercontrol itself can not have a duplicate name however - you do not have to have a x:Name on user control itself. Your visual states could use a top level <Panel> or scroll view which is wrapped by the user control.
I hope this would help.
regards,
Digvijay
-
Sunday, October 09, 2011 8:50 AM
Thanks for the reply. Unfortunately, I don't understand how this would work:
The usercontrol itself can not have a duplicate name however - you do not have to have a x:Name on user control itself. Your visual states could use a top level <Panel> or scroll view which is wrapped by the user control.
Could you offer an example?
Figured it out, I think.
-
Wednesday, April 25, 2012 9:04 AM
This is a fantastic walkthrough, thanks for sharing. I recently had this problem and it was also the Name set in code on a FrameworkElement added to UIElementCollection.

