This is a migrated thread and some comments may be shown as answers.

Drag and Drop Issue

8 Answers 177 Views
DragAndDrop
This is a migrated thread and some comments may be shown as answers.
Manas Rastogi
Top achievements
Rank 1
Manas Rastogi asked on 14 Aug 2009, 11:16 AM

hi,
I tried to execute your sample code for drag and drop but unfortunately it is throwing me some error.
In the line ListBox box = ItemsControl.ItemsControlFromItemContainer(listBoxItem) as ListBox;,it is giving error as
Error 1 'System.Windows.Controls.ItemsControl' does not contain a definition for 'ItemsControlFromItemContainer' C:\Documents and Settings\manas.rastogi\Desktop\Telerik Drag And Drop\Telerik Drag And Drop\Page.xaml.cs 127 64 Telerik Drag And Drop
i have added given the required references but still facing the same problem.

Page.xaml is:

 

<U

 

serControl x:Class="Telerik_Drag_And_Drop.Page"

 

 

 

 

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

 

 

 

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

 

 

 

Width="400" Height="300"

 

 

 

 

xmlns:panels="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"

 

 

 

 

xmlns:primitives="clr-namespace:Telerik.Windows.Controls.Primitives;assembly=Telerik.Windows.Controls"

 

 

 

 

xmlns:dragDrop="clr-namespace:Telerik.Windows.Controls.DragDrop;assembly=Telerik.Windows.Controls">

 

 

 

 

 

<UserControl.Resources>

 

 

 

 

<DataTemplate x:Key="ApplicationDragTemplate">

 

 

 

 

<Image Source="{Binding IconPath}" Stretch="None" VerticalAlignment="Top" />

 

 

 

 

</DataTemplate>

 

 

 

 

<Style TargetType="ListBoxItem" x:Key="draggableItemStyle">

 

 

 

 

<Setter Property="HorizontalAlignment" Value="Stretch" />

 

 

 

 

<Setter Property="HorizontalContentAlignment" Value="Stretch" />

 

 

 

 

<Setter Property="dragDrop:RadDragAndDropManager.AllowDrag" Value="True" />

 

 

 

 

</Style>

 

 

 

 

</UserControl.Resources>

 

 

  

 

<Grid x:Name="LayoutRoot">

 

 

 

 

<Grid.ColumnDefinitions>

 

 

 

 

<ColumnDefinition Width="200" />

 

 

 

 

<ColumnDefinition Width="150" />

 

 

 

 

<ColumnDefinition Width="*" />

 

 

 

 

</Grid.ColumnDefinitions>

 

 

 

 

<!--All Applications-->

 

 

 

 

<Border CornerRadius="5" BorderBrush="Black" BorderThickness="5" Background="#FFFFFFFF">

 

 

 

 

<Grid>

 

 

 

 

<Border Height="30" VerticalAlignment="Top">

 

 

 

 

<Border.Background>

 

 

 

 

<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

 

 

 

 

<GradientStop Color="#FF4C4C4C" />

 

 

 

 

<GradientStop Color="#FF333538" Offset="1" />

 

 

 

 

<GradientStop Color="#FF3C3D3F" Offset="0.394" />

 

 

 

 

<GradientStop Color="#FF151516" Offset="0.417" />

 

 

 

 

</LinearGradientBrush>

 

 

 

 

</Border.Background>

 

 

 

 

<TextBlock Text="All Applications:" Margin="10,5" Foreground="#FFFFFFFF"

 

 

 

 

FontSize="12" />

 

 

 

 

</Border>

 

 

 

 

<ListBox x:Name="allApplicationsBox" Margin="10 40 10 10" BorderThickness="0"

 

 

 

 

dragDrop:RadDragAndDropManager.AllowDrop="True"

 

 

 

 

ItemContainerStyle="{StaticResource draggableItemStyle}">

 

 

 

 

<ListBox.ItemTemplate>

 

 

 

 

<DataTemplate>

 

 

 

 

<Grid Width="140">

 

 

 

 

<Grid.RowDefinitions>

 

 

 

<RowDefinition />

 

 

 

<RowDefinition />

 

 

 

<RowDefinition />

 

 

 

 

</Grid.RowDefinitions>

 

 

 

 

<Image Grid.Row="0" HorizontalAlignment="Center"

 

 

 

 

Source="{Binding IconPath}" Width="32" Height="32"

 

 

 

 

Margin="0 0 5 0" />

 

 

 

 

<TextBlock Grid.Row="1" Text="{Binding Name}" FontWeight="Bold"

 

 

 

 

HorizontalAlignment="Center" />

 

 

 

 

<TextBlock Text="{Binding Author}" Grid.Row="2"

 

 

 

 

HorizontalAlignment="Center" />

 

 

 

 

</Grid>

 

 

 

 

</DataTemplate>

 

 

 

 

</ListBox.ItemTemplate>

 

 

 

 

</ListBox>

 

 

 

 

</Grid>

 

 

 

 

</Border>

 

 

 

 

<!--Text-->

 

 

 

 

<Border Grid.Column="1" BorderBrush="Gray" Background="LightGray" BorderThickness="1"

 

 

 

 

VerticalAlignment="Center" HorizontalAlignment="Center">

 

 

 

 

<TextBlock Text="Drag applications from All Applications to My Applications."

 

 

 

HorizontalAlignment="Center" TextWrapping="Wrap" Margin="10" FontSize="10" />

 

 

 

</Border>

 

 

 

 

<!--My Applications-->

 

 

 

 

<Border CornerRadius="5" Grid.Column="2" BorderBrush="Black" BorderThickness="5"Background="#FFFFFFFF">

 

 

 

 

<Grid>

 

 

 

 

<Border Height="30" VerticalAlignment="Top">

 

 

 

 

<Border.Background>

 

 

 

 

<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

 

 

 

 

<GradientStop Color="#FF4C4C4C" />

 

 

 

 

<GradientStop Color="#FF333538" Offset="1" />

 

 

 

 

<GradientStop Color="#FF3C3D3F" Offset="0.394" />

 

 

 

<GradientStop Color="#FF151516" Offset="0.417" />

 

 

 

</LinearGradientBrush>

 

 

 

 

</Border.Background>

 

 

 

 

<TextBlock Text="My Applications:" Margin="10,5" Foreground="#FFFFFFFF" FontSize="12" />

 

 

 

 

</Border>

 

 

 

 

<ListBox x:Name="myApplicationsBox" Margin="10 40 10 10"

 

 

 

 

dragDrop:RadDragAndDropManager.AllowDrop="True" BorderThickness="0"

 

 

 

 

ItemContainerStyle="{StaticResource draggableItemStyle}">

 

 

 

 

<ListBox.ItemTemplate>

 

 

 

<DataTemplate>

 

 

 

<StackPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch">

 

 

 

 

<Image Source="{Binding IconPath}" Margin="0 0 3 0"

 

 

 

 

HorizontalAlignment="Center" />

 

 

 

 

<TextBlock Text="{Binding Name}" HorizontalAlignment="Center" />

 

 

 

 

</StackPanel>

 

 

 

 

</DataTemplate>

 

 

 

 

</ListBox.ItemTemplate>

 

 

 

 

<ListBox.ItemsPanel>

 

 

 

 

<ItemsPanelTemplate>

 

 

 

 

<primitives:RadUniformGrid Columns="3" HorizontalAlignment="Left"

 

 

 

 

VerticalAlignment="Top" />

 

 

 

 

</ItemsPanelTemplate>

 

 

 

 

</ListBox.ItemsPanel>

 

 

 

 

</ListBox>

 

 

 

 

 </Grid>

 

 

 

 

 </Border>

 

 

 

 

</Grid>

 

 

 

 

</UserControl>

 

 

 

 

 

 

 

 The Page.xaml.cs is:

using System;

 

using System.Collections.Generic;

 

 

u

sing System.Collections.ObjectModel;

 

 

 

using System.ComponentModel;

 

 

 

using System.Windows;

 

 

 

using System.Windows.Controls;

 

 

 

using System.Windows.Controls.Primitives;

 

 

 

using System.Windows.Controls.Test;

 

 

 

using System.Windows.Media;

 

 

 

using Telerik.Windows.Controls.DragDrop;

 

 

 

 

 

 

 

namespace 

 

Telerik_Drag_And_Drop

 

 

 {

 

public partial class Page : UserControl

 

 

 

 

 {

 

 

 

public static ObservableCollection<ApplicationInfo> GenerateApplicationInfos()

 

 

 {

 

return new ObservableCollection<ApplicationInfo>()

 

 

{

 

new ApplicationInfo()

 

 

 {

Name =

"Large Collider",

 

 

Author =

"C.E.R.N.",

 

 

 IconPath =

@"/DragAndDrop/Images/LargeIcons/Atom.png"

 

 

 

 

 },

 

 

 

new ApplicationInfo()

 

 

{

Name =

"Paintbrush",

 

 

 Author =

"Imagine Inc.",

 

 

 IconPath =

@"/DragAndDrop/Images/LargeIcons/Brush.png"

 

 

 

 

 },

 

 

 

new ApplicationInfo()

 

 

 {

Name =

"Lively Calendar",

 

 

 Author =

"Control AG",

 

 

 IconPath =

@"/DragAndDrop/Images/LargeIcons/CalendarEvents.png"

 

 

 

 },

 

 

new ApplicationInfo()

 

{

Name =

"Fire Burning ROM",

 

 

 Author =

"The CD Factory",

 

 

 IconPath =

@"/DragAndDrop/Images/LargeIcons/CDBurn.png"

 

 

 

 

 },

 

 

new ApplicationInfo()

 

 

{

Name =

"Fav Explorer",

 

 

 Author =

"Star Factory",

 

 

 IconPath =

@"/DragAndDrop/Images/LargeIcons/favorites.png"

 

 

 

 

 },

 

 

 

new ApplicationInfo()

 

 

{

Name =

"IE Fox",

 

 

 Author =

"Open Org",

 

 

 IconPath = @

"/DragAndDrop/Images/LargeIcons/Connected.png"

 

 

 

 },

 

 

new ApplicationInfo()

 

{

Name =

"Charting",

 

 

 Author =

"AA-AZ inc",

 

 

 IconPath =

@"/DragAndDrop/Images/LargeIcons/ChartDot.png"

 

 

 

 

 },

 

 

 

new ApplicationInfo()

 

 

 {

Name =

"SuperPlay",

 

 

 Author =

"EB Games",

 

 

 IconPath =

@"/DragAndDrop/Images/LargeIcons/Games.png"

 

 

 

 

 }

 

};

}

 

ObservableCollection<ApplicationInfo> allApplications = GenerateApplicationInfos();

 

 

 

ObservableCollection<ApplicationInfo> myApplications;

 

 

 

Brush listBoxDragPossible = new SolidColorBrush(Colors.Orange);

 

 

 

public Page()

 

 

 {

InitializeComponent();

myApplications =

 

 

new ObservableCollection<ApplicationInfo>();

 allApplicationsBox.ItemsSource = allApplications;

myApplicationsBox.ItemsSource = myApplications;

 

RadDragAndDropManager.AddDragQueryHandler(this, OnDragQuery);

 

 

 

RadDragAndDropManager.AddDragInfoHandler(this, OnDragInfo);

 

 

 

RadDragAndDropManager.AddDropQueryHandler(this, OnDropQuery);

 

 

 

RadDragAndDropManager.AddDropInfoHandler(this, OnDropInfo);

 

 

 }

 

private void OnDropInfo(object sender, DragDropEventArgs e)

 

 

 {

 

ItemsControl box = e.Options.Destination as ItemsControl;

 

 

 

IList<ApplicationInfo> itemsSource = box.ItemsSource as IList<ApplicationInfo>;

 

 

 

ApplicationInfo payload = e.Options.Payload as ApplicationInfo;

 

 

 

if (e.Options.Status == DragStatus.DropPossible)

 

 

 {

box.BorderBrush = listBoxDragPossible;

}

 

else

 

 

 

 

{

 

 

box.BorderBrush =

 

 

new SolidColorBrush(Colors.Gray);

}

 

if (e.Options.Status == DragStatus.DropComplete)

 

 

 {

 

if (!itemsSource.Contains(payload))

 

 

 {

itemsSource.Add(payload);

}

}

}

 

void OnDropQuery(object sender, DragDropQueryEventArgs e)

 

 

 {

 

ItemsControl box = e.Options.Destination as ItemsControl;

 

 

 

IList<ApplicationInfo> itemsSource = box.ItemsSource as IList<ApplicationInfo>;

 

 

 

ApplicationInfo payload = e.Options.Payload as ApplicationInfo;

 

 

 e.QueryResult = payload !=

null && !itemsSource.Contains(payload);

 

 

 }

 

void OnDragInfo(object sender, DragDropEventArgs e)

 

 

 {

 

ListBoxItem listBoxItem = e.Options.Source as ListBoxItem;

 

 

 

ListBox box = System.Windows.Controls.ItemsControl.ItemsControlFromItemContainer(listBoxItem) as ListBox;

 

 

 

IList<ApplicationInfo> itemsSource = box.ItemsSource as IList<ApplicationInfo>;

 

 

 

ApplicationInfo payload = e.Options.Payload as ApplicationInfo;

 

 

 

if (e.Options.Status == DragStatus.DragComplete)

 

 

 {

 

if (payload != null && itemsSource.Contains(payload))

 

 

 {

itemsSource.Remove(payload);

}

}

}

 

protected virtual void OnDragQuery(object sender, DragDropQueryEventArgs e)

 

 

 {

 L

istBoxItem listBoxItem = e.Options.Source as ListBoxItem;

 

 

 

ListBox box = ItemsControl.ItemsControlFromItemContainer(listBoxItem) as ListBox;

 

 

 

if (e.Options.Status == DragStatus.DragQuery && box != null)

 

 

 {

e.Options.Payload = box.SelectedItem;

 

ContentControl cue = new ContentControl();

 

 

 cue.ContentTemplate =

this.Resources["ApplicationDragTemplate"] as DataTemplate;

 

 

 cue.Content = box.SelectedItem;

e.Options.DragCue = cue;

e.Options.ArrowCue =

 

 

RadDragAndDropManager.GenerateArrowCue();

 }

e.QueryResult =

true;

 

 

 }

}

 

public class ApplicationInfo

 

 

 

 

{

 

 

 

public Double Price

 

 

 {

 

get;

 

 

 

set;

 

 

 }

 

public String IconPath

 

 

 {

 

get;

 

 

 

set;

 

 

 }

 

public String Name

 

 

{

 

get;

 

 

 

set;

 

 

}

 

public String Author

 

 

 {

 

get;

 

 

  

set;

 

 

 }

}

}

 

 

8 Answers, 1 is accepted

Sort by
0
Valentin.Stoychev
Telerik team
answered on 14 Aug 2009, 11:21 AM
Hello Manas Rastogi,

You need to use the Telerik.Windows.Controls.ItemsControl, not the base one. The methods you try to call are in there.

All the best,
Valentin.Stoychev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Manas Rastogi
Top achievements
Rank 1
answered on 14 Aug 2009, 11:45 AM
hi Valentin.Stoychev
thanks for the reply,but i am unable to give Telerik.Windows.Controls.ItemsControl as it not recognising itemscontrol in Telerik.Windows.Controls.
0
Valentin.Stoychev
Telerik team
answered on 14 Aug 2009, 11:56 AM
Hello Manas Rastogi,

Hm - this is strange. Can you please double check your code. All our controls rely on it - so it is there for sure.

Have you added a reference to Telerik.Windows.Controls dll and do you have:
using Telerik.Windows.Controls;

in your class? 

Let us know how it goes.

Best wishes,
Valentin.Stoychev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Manas Rastogi
Top achievements
Rank 1
answered on 14 Aug 2009, 12:06 PM
hi Valentin.Stoychev
i changed the code line to

ListBox

 

box = Telerik.Windows.Controls.ItemsControl.ItemsControlFromItemContainer(listBoxItem) as ListBox;

 

but it gives an error as:
Cannot convert type 'Telerik.Windows.Controls.ItemsControl' to 'System.Windows.Controls.ListBox' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion C:\Documents and Settings\manas.rastogi\Desktop\Telerik Drag And Drop\Telerik Drag And Drop\Page.xaml.cs 128 27 Telerik Drag And Drop

0
Manas Rastogi
Top achievements
Rank 1
answered on 14 Aug 2009, 12:15 PM
hi Valentin.Stoychev
i solved that error but something  else is now cropping up.Can you provide me with a working sample demonstrating drag and drop the way it is shown in your demo.
Thanks
Manas
0
Bobi
Telerik team
answered on 14 Aug 2009, 01:37 PM
Hello Manas Rastogi,

Please find attached the example from our online demo.
Be aware that you need to add "using Telerik.Windows" if you use it in thsi way:
     public event EventHandler<DragDropQueryEventArgs> DragQuery
        {
            add
            {
                this.AddHandler(RadDragAndDropManager.DragQueryEvent, value);
            }
            remove
            {
                this.RemoveHandler(RadDragAndDropManager.DragQueryEvent, value);
            }
        }

I hope that this will help you.

Greetings,
Boryana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Manas Rastogi
Top achievements
Rank 1
answered on 17 Aug 2009, 06:06 AM
hi Boryana,Valentin.Stoychev
Thanks for providing me with the sample draganddropexample.zip.But here also i had to change the following lines:

ListBoxItem

 

listBoxItem = e.Options.Source as ListBoxItem;

 

 

 

ListBox box = ItemsControl.ItemsControlFromItemContainer(listBoxItem) as ListBox;

 

 

 to

Telerik.Windows.Controls.

ListBoxItem listBoxItem = e.Options.Source as Telerik.Windows.Controls.ListBoxItem;

 

Telerik.Windows.Controls.

 

ListBox box = Telerik.Windows.Controls.ItemsControl.ItemsControlFromItemContainer(listBoxItem) as Telerik.Windows.Controls.ListBox;

 

 


else it was showing an exception that 'System.Windows.Controls.ItemsControl' does not contain a definition for 'ItemsControlFromItemContainer'.Now after changing,the following variables listBoxItem,box,itemsSource turn out to be null in the method

void

 

OnDragInfo(object sender, DragDropEventArgs e).My exception is NullReferenceException was unhandled by UserCode.
Please help me with this.

 

 

0
Tihomir Petkov
Telerik team
answered on 17 Aug 2009, 03:22 PM
Hello,

The ItemsControlFromItemContainer() method is available in the base ItemsControl class with the release of Silverlight 3 and is missing in older versions of the runtime. From what you are saying it seems that you are developing against the Silverlight 2 runtime. Is there any specific reason why you are not working with the official version of Silverlight?

As a side note, with our latest release 2009 Q2 SP1 we officially ended the support for the now obsolete Silverlight 2 version of the runtime.

All the best,
Tihomir Petkov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
DragAndDrop
Asked by
Manas Rastogi
Top achievements
Rank 1
Answers by
Valentin.Stoychev
Telerik team
Manas Rastogi
Top achievements
Rank 1
Bobi
Telerik team
Tihomir Petkov
Telerik team
Share this question
or