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;
}
}
}