"System.ArgumentException: Value does not fall within the expected range" on drop

Jul 1, 2009 at 9:23 PM
Edited Jul 1, 2009 at 9:53 PM

When I drop a DragSource onto a DropTarget, I get that exception on the following line:

newChildParentDropTarget.MainContentControl.Children.Add(currentChild); (line 376 in the latest source)

The following is the relevant XAML for my project:

<Grid Name="TaskLayout" Width="840" Margin="160, 0, 0, 0">
		<TextBlock Text="Not Started" Width="200" HorizontalAlignment="Left" VerticalAlignment="Top" Height="25" FontSize="16" Margin="50, 0, 0, 0" />
		<ListBox ItemsSource="{Binding NotStartedTasks}" Width="200" Tag="1" HorizontalAlignment="Left" Height="500" VerticalAlignment="Bottom" Margin="0, 0, 0, 0">
			<ListBox.ItemTemplate>
				<DataTemplate>
					<dd:DropTarget Ghost="{Binding DropTargetGhost}" DragSourceDropped="DropTarget_DragSourceDropped">
						<dd:DropTarget.Content>
							<dd:DragSource AllDropTargetsValid="True" Ghost="{Binding DragSourceGhost}" HorizontalAlignment="Left" VerticalAlignment="Top" DragHandleMode="FullDragSource">
								<dd:DragSource.Content>
									<Grid Width="175" Height="110" Background="White">
										<TextBlock Text="{Binding Name}" MaxWidth="175" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center" />
									</Grid>
								</dd:DragSource.Content>
							</dd:DragSource>
						</dd:DropTarget.Content>
					</dd:DropTarget>
				</DataTemplate>
			</ListBox.ItemTemplate>
		</ListBox>
		<TextBlock Text="In Progress" Width="200" HorizontalAlignment="Left" VerticalAlignment="Top" Height="25" FontSize="16" Margin="250, 0, 0, 0" />
		<ListBox ItemsSource="{Binding InProgressTasks}" Width="200" Tag="2" HorizontalAlignment="Left" Height="500" VerticalAlignment="Bottom" Margin="198, 0, 0, 0">
			<ListBox.ItemTemplate>
				<DataTemplate>
					<dd:DropTarget Ghost="{Binding DropTargetGhost}" DragSourceDropped="DropTarget_DragSourceDropped">
						<dd:DropTarget.Content>
							<dd:DragSource AllDropTargetsValid="True" Ghost="{Binding DragSourceGhost}" HorizontalAlignment="Left" VerticalAlignment="Top" DragHandleMode="FullDragSource">
								<dd:DragSource.Content>
									<Grid Width="175" Height="110" Background="White">
										<TextBlock Text="{Binding Name}" MaxWidth="175" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center" />
									</Grid>
								</dd:DragSource.Content>
							</dd:DragSource>
						</dd:DropTarget.Content>
					</dd:DropTarget>
				</DataTemplate>
			</ListBox.ItemTemplate>
		</ListBox>
	</Grid>

 

When this error occurs, the object that was in that DropTarget gets overridden with the old object, but it still shows the switched object's name until you click on the DragSource, so it looks like it's reverting the switch on one of the objects, but not both.  Any fixes for this, or am I doing something wrong in the XAML?

 

Jul 26, 2009 at 2:18 PM

This problem usually occurs when the objects added dont have a unique name. Try assigning unique names to all the objects and that should help your problem.