We meet an exception when we try to drag list box item(A custom control) from one list box to another list box. The two list boxes are in the same list box.
<ListBox x:Name="ListBox" Margin="0,0,3,0"
AllowDrop="True"
ItemContainerStyle="{StaticResource draggableItemStyle}">
<telerik:ListBoxDragDrop.Behavior>
<telerik:ListBoxDragDropBehavior />
</telerik:ListBoxDragDrop.Behavior>
<telerik:ListBoxDragDrop.DragVisualProvider>
<local:DragVisualProvider />
</telerik:ListBoxDragDrop.DragVisualProvider>
</ListBox>
The detail error information is below:
Value does not fall within the expected range.
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_Insert[T](PresentationFrameworkCollection`1 collection, Int32 index, Object value)
at System.Windows.PresentationFrameworkCollection`1.InsertImpl(Int32 index, Object value)
at System.Windows.Controls.ItemCollection.InsertImpl(Int32 index, Object value)
at System.Windows.PresentationFrameworkCollection`1.System.Collections.IList.Insert(Int32 index, Object value)
at Telerik.Windows.DragDrop.Behaviors.DragDropBehavior`1.InsertItems(IList source, Int32 index, IEnumerable items)
at Telerik.Windows.DragDrop.Behaviors.DragDropBehavior`1.Drop(TState state)
at Telerik.Windows.DragDrop.Behaviors.DragDropHelper`2.Drop(FrameworkElement dropTarget, FrameworkElement dropItemsControl, Object data, Type itemType)
at Telerik.Windows.DragDrop.Behaviors.DragDropHelper`2.Drop(Object sender, DragEventArgs e)
at Telerik.Windows.DragDrop.DragEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at Telerik.Windows.RadRoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at Telerik.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RadRoutedEventArgs routedEventArgs)
at Telerik.Windows.EventRoute.InvokeHandlersImpl(Object source, RadRoutedEventArgs args, Boolean raisedAgain)
at Telerik.Windows.RadRoutedEventHelper.RaiseEvent(DependencyObject element, RadRoutedEventArgs args)
at Telerik.Windows.DragDrop.DragOperation.RaiseDragEvent(RoutedEvent dragEvent, DependencyObject target)
at Telerik.Windows.DragDrop.DragOperation.OnDrop(DependencyObject source)
at Telerik.Windows.DragDrop.DragOperation.DragSourceLostMouseCapture(Object sender, MouseEventArgs e)
at Telerik.Windows.DragDrop.DragHelper.OnLostMouseCapture(Object sender, MouseEventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(UInt32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName, UInt32 flags)
3 Answers, 1 is accepted
I have tested your code snippet, but I was not able to reproduce such an issue. May I ask you to test with the latest binaries. If the problem still remains may I ask you to share the full implementation you have done?
Regards,Didie
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
Catalogue.xaml
<UserControl x:Class="CatalogueUserControl.Catalogue"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
xmlns:local="clr-namespace:CatalogueUserControl"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:telerikDragDrop="clr-namespace:Telerik.Windows.Controls.DragDrop;assembly=Telerik.Windows.Controls">
<UserControl.Resources>
<Style TargetType="ListBoxItem" x:Key="WishlistItemStyle" >
<Setter Property="telerik:DragDropManager.AllowCapturedDrag" Value="True" />
</Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" ShowGridLines="True" Width="Auto" HorizontalAlignment="Stretch" Margin="0,20,0,20" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" x:Name="CatalogueGrid" Background="Black" Width="Auto" MouseLeftButtonUp="CatalogueGrid_MouseLeftButtonUp" MouseRightButtonUp="CatalogueGrid_MouseRightButtonUp" MouseRightButtonDown="CatalogueGrid_MouseRightButtonDown">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="30*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="15*"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="CatalogueTitle" TextWrapping="Wrap" Text="Catalogue" Padding="0,1" FontSize="16" Foreground="White" Background="Black" Grid.Column="0" MinWidth="300"/>
<TextBlock x:Name="WeightTexbBlock" TextWrapping="Wrap" Text="Weight:" FontSize="16" VerticalAlignment="Center" Foreground="White" Grid.Column="2" />
<TextBox x:Name="WeightTextbox" TextWrapping="Wrap" Text="" Foreground="White" Background="#FF433F3F" FontSize="16" BorderThickness="0" HorizontalAlignment="Left" Grid.Column="3" />
<Button x:Name="CatalogueGroupButton" Content="Group Settings" HorizontalAlignment="Stretch" FontSize="16" Grid.Column="4"/>
<Button x:Name="AddCatalogue" Content="Add Catalogue" FontSize="16" HorizontalAlignment="Stretch" Click="AddCatalogue_Click" Grid.Column="6"/>
<Button x:Name="AddCritiera" Content="Add Critiera" FontSize="16" HorizontalAlignment="Stretch" Grid.Column="8" Click="AddCritiera_Click" />
</Grid>
<Grid Grid.Row="1" x:Name="CatalogueContent" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0" SizeChanged="CatalogueContent_SizeChanged">
<Grid.ColumnDefinitions>
</Grid.ColumnDefinitions>
<ListBox x:Name="ListBox" Margin="0,0,3,0"
AllowDrop="True"
ItemContainerStyle="{StaticResource WishlistItemStyle}"
local:ListBoxDragDropBehavior.IsEnabled="True">
</ListBox>
</Grid>
<Grid x:Name="TotolGrid" Margin="0" Background="Black" Grid.Row="2" >
<toolkit:WrapPanel VerticalAlignment="Center">
<TextBlock TextWrapping="Wrap" Text="Total Score:" Foreground="White" FontSize="16" VerticalAlignment="Center" Margin="5,0,0,0"/>
<TextBox x:Name="Total_Score" TextWrapping="Wrap" Background="Black" Foreground="White" VerticalAlignment="Center" FontSize="16" Margin="5,0,0,0" IsReadOnly="True" TextChanged="Total_Score_TextChanged" />
</toolkit:WrapPanel>
</Grid>
</Grid>
</UserControl>
Catalogue.xaml.cs
public partial class Catalogue : UserControl
{
public Catalogue()
{
InitializeComponent();
}
private void AddCatalogue_Click(object sender, RoutedEventArgs e)
{
AddCatalogueFunc();
}
public Catalogue AddCatalogueFunc(bool isSetWidth = false, double myWidth = 0.0)
{
Catalogue catalogue = null;
if (mCatalogue)
{
mCritiera = false;
catalogue = new Catalogue();
catalogue.canRemove = true;
catalogue.parentCatalogue = this;
catalogue.Name = Guid.NewGuid().ToString().Replace('-','_');
if (isSetWidth)
{
catalogue.mCatalogueContentWidth = myWidth * 0.95;
catalogue.Width = myWidth * 0.95;
}
else
{
catalogue.mCatalogueContentWidth = CatalogueContent.ActualWidth * 0.95;
catalogue.Width = catalogue.mCatalogueContentWidth;
}
ListBox.Items.Add(catalogue);
}
return catalogue;
}
}
Mainpage.xaml
<UserControl x:Class="CatalogueUserControl.MainPage"
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:local="clr-namespace:CatalogueUserControl">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="100*"/>
</Grid.RowDefinitions>
<local:Catalogue Height="Auto" Grid.Row="0" Margin="0,0,0,0" x:Name="RootCatalogue" />
<Button Content="Button" Height="9" HorizontalAlignment="Left" Margin="231,90,0,0" Name="button1" VerticalAlignment="Top" Width="46" Click="button1_Click" />
</Grid>
</ScrollViewer>
</UserControl>
Mainpage.xmal
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
}
QuestionInCriteria.xaml
<UserControl
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:local="clr-namespace:CatalogueUserControl"
mc:Ignorable="d"
x:Class="CatalogueUserControl.QuestionsInCritiera"
d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<Grid Margin="0" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.673*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="67"/>
</Grid.ColumnDefinitions>
<local:UnderlineTextbox x:Name="Rating" Grid.Column="2" Margin="8,0" VerticalAlignment="Center" Width="51" IsEnabled="False"/>
<TextBox Grid.Column="0" x:Name="Criteria" Margin="0" TextWrapping="Wrap" Text="TextBox" BorderBrush="{x:Null}"/>
<local:WeightControl x:Name="CritieraWeight" Margin="5.5,0,5,0" d:LayoutOverrides="Width, Height" Grid.Column="1"/>
</Grid>
</Grid>
</UserControl>
Underlinetextbox.xaml
<UserControl 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:Microsoft_Internal_Pivot_Controls="clr-namespace:Microsoft.Internal.Pivot.Controls;assembly=System.Windows.Controls.Pivot" xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" x:Name="UnderlineTextbox1"
x:Class="CatalogueUserControl.UnderlineTextbox"
d:DesignWidth="640" mc:Ignorable="d" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<Microsoft_Internal_Pivot_Controls:EnforcingStackPanel x:Name="StackPanel" Margin="0" VerticalAlignment="Top">
<TextBox x:Name="Text_Input" Text="5.0" BorderThickness="1" BorderBrush="{x:Null}" Padding="2,2,2,0" />
<Rectangle Fill="#FFF4F4F5" Height="2" Margin="0" Stroke="Black" VerticalAlignment="Top"/>
</Microsoft_Internal_Pivot_Controls:EnforcingStackPanel>
</Grid>
</UserControl>
WeightControl.xaml
<UserControl 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:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" xmlns:local="clr-namespace:CatalogueUserControl"
x:Class="CatalogueUserControl.WeightControl"
mc:Ignorable="d"
d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<toolkit:WrapPanel Margin="0" VerticalAlignment="Center">
<TextBlock TextWrapping="Wrap" Text="Weight:" VerticalAlignment="Center" HorizontalAlignment="Left"/>
<local:UnderlineTextbox x:Name="Weight" Width="27"/>
<Button x:Name="GroupSettings" Content="GroupSettings" Margin="20,0,0,0"/>
</toolkit:WrapPanel>
</Grid>
</UserControl>
listBoxDragDropBehavior.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using Telerik.Windows.DragDrop;
using Telerik.Windows.DragDrop.Behaviors;
using System.Windows.Controls;
using System.Diagnostics.CodeAnalysis;
namespace CatalogueUserControl
{
[SuppressMessage("Microsoft.Naming", "CA1714:FlagsEnumsShouldHavePluralNames")]
[Flags]
public enum DropPosition
{
// Summary:
// Item will be dropeed before the target.
Before = 1,
//
// Summary:
// Item will be dropeed before inside the target.
Inside = 2,
//
// Summary:
// Item will be dropeed after the target.
After = 4,
}
public class ListBoxDragDropBehavior
{
private ListBox _associatedObject;
/// <summary>
/// AssociatedObject Property
/// </summary>
public ListBox AssociatedObject
{
get
{
return _associatedObject;
}
set
{
_associatedObject = value;
}
}
private static Dictionary<ListBox, ListBoxDragDropBehavior> instances;
static ListBoxDragDropBehavior()
{
instances = new Dictionary<ListBox, ListBoxDragDropBehavior>();
}
public static bool GetIsEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsEnabledProperty);
}
public static void SetIsEnabled(DependencyObject obj, bool value)
{
ListBoxDragDropBehavior behavior = GetAttachedBehavior(obj as ListBox);
behavior.AssociatedObject = obj as ListBox;
if (value)
{
behavior.Initialize();
}
else
{
behavior.CleanUp();
}
obj.SetValue(IsEnabledProperty, value);
}
// Using a DependencyProperty as the backing store for IsEnabled. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsEnabledProperty =
DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(ListBoxDragDropBehavior),
new PropertyMetadata(new PropertyChangedCallback(OnIsEnabledPropertyChanged)));
public static void OnIsEnabledPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
SetIsEnabled(dependencyObject, (bool)e.NewValue);
}
private static ListBoxDragDropBehavior GetAttachedBehavior(ListBox listBox)
{
if (!instances.ContainsKey(listBox))
{
instances[listBox] = new ListBoxDragDropBehavior();
instances[listBox].AssociatedObject = listBox;
}
return instances[listBox];
}
protected virtual void Initialize()
{
this.UnsubscribeFromDragDropEvents();
this.SubscribeToDragDropEvents();
}
protected virtual void CleanUp()
{
this.UnsubscribeFromDragDropEvents();
}
private void SubscribeToDragDropEvents()
{
DragDropManager.AddDragInitializeHandler(this.AssociatedObject, OnDragInitialize);
DragDropManager.AddGiveFeedbackHandler(this.AssociatedObject, OnGiveFeedback);
DragDropManager.AddDropHandler(this.AssociatedObject, OnDrop);
DragDropManager.AddDragDropCompletedHandler(this.AssociatedObject, OnDragDropCompleted);
DragDropManager.AddDragOverHandler(this.AssociatedObject, OnDragOver);
}
private void UnsubscribeFromDragDropEvents()
{
DragDropManager.RemoveDragInitializeHandler(this.AssociatedObject, OnDragInitialize);
DragDropManager.RemoveGiveFeedbackHandler(this.AssociatedObject, OnGiveFeedback);
DragDropManager.RemoveDropHandler(this.AssociatedObject, OnDrop);
DragDropManager.RemoveDragDropCompletedHandler(this.AssociatedObject, OnDragDropCompleted);
DragDropManager.RemoveDragOverHandler(this.AssociatedObject, OnDragOver);
}
private void OnDragInitialize(object sender, DragInitializeEventArgs e)
{
DropIndicationDetails details = new DropIndicationDetails();
var item = (sender as ListBox).SelectedItem;
details.CurrentDraggedItem = item;
IDragPayload dragPayload = DragDropPayloadManager.GeneratePayload(null);
dragPayload.SetData("DraggedData", item);
dragPayload.SetData("DropDetails", details);
e.Data = dragPayload;
e.DragVisual = new DragVisual()
{
Content = details,
ContentTemplate = this.AssociatedObject.Resources["DraggedItemTemplate"] as DataTemplate
};
e.DragVisualOffset = e.RelativeStartPoint;
e.AllowedEffects = DragDropEffects.All;
}
private void OnGiveFeedback(object sender, Telerik.Windows.DragDrop.GiveFeedbackEventArgs e)
{
e.SetCursor(Cursors.Arrow);
e.Handled = true;
}
private void OnDragDropCompleted(object sender, Telerik.Windows.DragDrop.DragDropCompletedEventArgs e)
{
var draggedItem = DragDropPayloadManager.GetDataFromObject(e.Data, "DraggedData");
if (e.Effects != DragDropEffects.None)
{
var collection = (sender as ListBox).Items as IList;
collection.Remove(draggedItem);
}
}
private void OnDrop(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
{
var draggedItem = DragDropPayloadManager.GetDataFromObject(e.Data, "DraggedData");
var details = DragDropPayloadManager.GetDataFromObject(e.Data, "DropDetails") as DropIndicationDetails;
if (details == null || (details == null && draggedItem == null))
{
return;
}
if (e.Effects != DragDropEffects.None)
{
var collection = (sender as ListBox).Items as IList;
collection.Add(draggedItem);
//collection.Add(new Catalogue());
}
}
private void OnDragOver(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
{
var draggedItem = DragDropPayloadManager.GetDataFromObject(e.Data, "DraggedData");
var itemsType = (this.AssociatedObject.Items as IList).AsQueryable().ElementType;
if (draggedItem.GetType() != itemsType)
{
e.Effects = DragDropEffects.None;
}
var dropDetails = DragDropPayloadManager.GetDataFromObject(e.Data, "DropDetails") as DropIndicationDetails;
dropDetails.CurrentDraggedOverItem = this.AssociatedObject;
dropDetails.CurrentDropPosition = DropPosition.Inside;
e.Handled = true;
}
}
}
DropIndicationDetails.cs
using System;
using Telerik.Windows.Controls;
namespace CatalogueUserControl
{
public class DropIndicationDetails : ViewModelBase
{
private object currentDraggedItem;
private DropPosition currentDropPosition;
private object currentDraggedOverItem;
public object CurrentDraggedOverItem
{
get
{
return currentDraggedOverItem;
}
set
{
if (this.currentDraggedOverItem != value)
{
currentDraggedOverItem = value;
OnPropertyChanged("CurrentDraggedOverItem");
}
}
}
public int DropIndex { get; set; }
public DropPosition CurrentDropPosition
{
get
{
return this.currentDropPosition;
}
set
{
if (this.currentDropPosition != value)
{
this.currentDropPosition = value;
OnPropertyChanged("CurrentDropPosition");
}
}
}
public object CurrentDraggedItem
{
get
{
return this.currentDraggedItem;
}
set
{
if (this.currentDraggedItem != value)
{
this.currentDraggedItem = value;
OnPropertyChanged("CurrentDraggedItem");
}
}
}
}
}
DragVisualProvier.cs
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.DragDrop.Behaviors;
using Telerik.Windows.DragDrop;
using Telerik.Windows.Controls;
using System.Linq;
namespace CatalogueUserControl
{
public class DragVisualProvider : DependencyObject, IDragVisualProvider
{
public DataTemplate DraggedItemTemplate
{
get
{
return (DataTemplate)GetValue(DraggedItemTemplateProperty);
}
set
{
SetValue(DraggedItemTemplateProperty, value);
}
}
public static readonly DependencyProperty DraggedItemTemplateProperty =
DependencyProperty.Register("DraggedItemTemplate", typeof(DataTemplate), typeof(DragVisualProvider), new PropertyMetadata(null));
public FrameworkElement CreateDragVisual(DragVisualProviderState state)
{
var visual = new DragVisual();
var theme = StyleManager.GetTheme(state.Host);
if (theme != null)
{
StyleManager.SetTheme(visual, theme);
}
//visual.Content = state.DraggedItems.OfType<object>().FirstOrDefault();
visual.ContentTemplate = this.DraggedItemTemplate;
return visual;
}
public Point GetDragVisualOffset(DragVisualProviderState state)
{
return state.RelativeStartPoint;
}
public bool UseDefaultCursors { get; set; }
}
}
I have changed your code to use the ItemsSource instead of Items collection before modifying the source collection.
private
void
OnDragDropCompleted(
object
sender, Telerik.Windows.DragDrop.DragDropCompletedEventArgs e)
{
var draggedItem = DragDropPayloadManager.GetDataFromObject(e.Data,
"DraggedData"
);
if
(e.Effects != DragDropEffects.None)
{
var collection = (sender
as
ListBox).ItemsSource
as
IList;
collection.Remove(draggedItem);
}
}
private
void
OnDrop(
object
sender, Telerik.Windows.DragDrop.DragEventArgs e)
...
var collection = (sender
as
ListBox).ItemsSource
as
IList;
collection.Add(draggedItem);
}
}I do not get any errors now. Please change those lines and let me know about the result.
Kind regards,
Didie
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.