or
<
UserControl
x:Class
=
"TelerikDragandDrop.MainWindow"
xmlns:telerik
=
"http://schemas.telerik.com/2008/xaml/presentation"
xmlns:telerikDragandDrop
=
"clr-namespace:TelerikDragandDrop"
>
<
UserControl.Resources
>
<
telerikDragandDrop:MainViewModel
x:Key
=
"ViewModel"
/>
<!--Note: With this style we make the ListBoxItems draggable:-->
<
Style
TargetType
=
"ListBoxItem"
x:Key
=
"WishlistItemStyle"
>
<
Setter
Property
=
"telerik:DragDropManager.AllowCapturedDrag"
Value
=
"True"
/>
<
Setter
Property
=
"telerik:DragDropManager.TouchDragTrigger"
Value
=
"TapAndHold"
/>
</
Style
>
<
Style
TargetType
=
"telerik:GridViewRow"
x:Key
=
"OrderItemStyle"
>
<
Setter
Property
=
"telerik:DragDropManager.AllowCapturedDrag"
Value
=
"True"
/>
<
Setter
Property
=
"telerik:DragDropManager.TouchDragTrigger"
Value
=
"TapAndHold"
/>
</
Style
>
<
Style
TargetType
=
"{x:Type TextBox}"
x:Key
=
"textBoxStyle"
>
<
Setter
Property
=
"telerik:DragDropManager.AllowCapturedDrag"
Value
=
"False"
/>
<
Setter
Property
=
"telerik:DragDropManager.TouchDragTrigger"
Value
=
"TapAndHold"
/>
<
Setter
Property
=
"Foreground"
Value
=
"#777777"
/>
</
Style
>
</
UserControl.Resources
>
<
Grid
x:Name
=
"LayoutRoot"
>
<
Grid.ColumnDefinitions
>
<
ColumnDefinition
Width
=
"*"
/>
<
ColumnDefinition
Width
=
"*"
/>
<
ColumnDefinition
Width
=
"*"
/>
</
Grid.ColumnDefinitions
>
<!--Order-->
<
Border
BorderBrush
=
"#C8C9CC"
BorderThickness
=
"2"
CornerRadius
=
"6"
Margin
=
"46 46 0 46"
Background
=
"White"
Grid.Column
=
"1"
>
<
DockPanel
>
<
Border
DockPanel.Dock
=
"Top"
Background
=
"{telerik:Windows8Resource ResourceKey=AccentBrush}"
CornerRadius
=
"4 4 0 0"
>
<
TextBlock
Text
=
"Order"
Foreground
=
"{telerik:Windows8Resource ResourceKey=MainBrush}"
Margin
=
"20 5 5 5"
FontSize
=
"14"
/>
</
Border
>
<!--NOTE: The GridView is a drop target, we set the AllowDrop to true.-->
<
telerik:RadGridView
x:Name
=
"orderView"
IsReadOnly
=
"True"
Grid.Column
=
"1"
Grid.Row
=
"0"
Margin
=
"0,0,0,5"
RowIndicatorVisibility
=
"Collapsed"
GroupRenderMode
=
"Flat"
ItemsSource
=
"{Binding AllApplications, Source={StaticResource ViewModel}}"
RowStyle
=
"{StaticResource OrderItemStyle}"
CanUserFreezeColumns
=
" False"
CanUserInsertRows
=
"False"
CanUserReorderColumns
=
"False"
CanUserSortColumns
=
"False"
ShowGroupPanel
=
"False"
Padding
=
"5"
AllowDrop
=
"True"
telerikDragandDrop:GridViewDragDropBehavior.IsEnabled
=
"True"
Height
=
"115"
>
<
telerik:RadGridView.Resources
>
<
DataTemplate
x:Key
=
"DraggedItemTemplate"
>
<
StackPanel
>
<
StackPanel
Orientation
=
"Horizontal"
>
<
TextBlock
Text
=
"Dragging:"
/>
<
TextBlock
Text
=
"{Binding CurrentDraggedItem.FirstName}"
FontWeight
=
"Bold"
/>
</
StackPanel
>
</
StackPanel
>
</
DataTemplate
>
</
telerik:RadGridView.Resources
>
</
telerik:RadGridView
>
</
DockPanel
>
</
Border
>
<!--Whishlist-->
<
Border
BorderBrush
=
"#C8C9CC"
BorderThickness
=
"2"
CornerRadius
=
"6"
Margin
=
"46"
Background
=
"White"
Grid.Column
=
"2"
>
<
DockPanel
>
<
Border
DockPanel.Dock
=
"Top"
Background
=
"{telerik:Windows8Resource ResourceKey=AccentBrush}"
CornerRadius
=
"4 4 0 0"
>
<
TextBlock
Text
=
"Wishlist"
Margin
=
"20 5 5 5"
Foreground
=
"{telerik:Windows8Resource ResourceKey=MainBrush}"
FontSize
=
"14"
/>
</
Border
>
<!--NOTE: The ListBox is a drop target, we set the AllowDrop to true.-->
<
ListBox
x:Name
=
"wishlistView"
Grid.Column
=
"1"
Grid.Row
=
"1"
SelectionMode
=
"Extended"
ItemsSource
=
"{Binding MyApplications, Source={StaticResource ViewModel}}"
FontSize
=
"11"
ItemContainerStyle
=
"{StaticResource WishlistItemStyle}"
Padding
=
"5"
AllowDrop
=
"True"
telerikDragandDrop:ListBoxDragDropBehavior.IsEnabled
=
"True"
telerik:TouchManager.IsTouchHitTestVisible
=
"True"
>
<
ListBox.Resources
>
<
DataTemplate
x:Key
=
"DraggedItemTemplate"
>
<
StackPanel
>
<
StackPanel
Orientation
=
"Horizontal"
>
<
TextBlock
Text
=
"Dragging:"
/>
<
TextBlock
Text
=
"{Binding CurrentDraggedItem.FirstName}"
FontWeight
=
"Bold"
/>
</
StackPanel
>
</
StackPanel
>
</
DataTemplate
>
</
ListBox.Resources
>
<
ListBox.ItemTemplate
>
<
DataTemplate
>
<
Grid
Width
=
"150"
>
<
Grid.RowDefinitions
>
<
RowDefinition
/>
<
RowDefinition
/>
<
RowDefinition
/>
</
Grid.RowDefinitions
>
<
TextBlock
Grid.Row
=
"1"
Text
=
"{Binding FirstName}"
FontWeight
=
"Bold"
HorizontalAlignment
=
"Center"
Foreground
=
"#FF767676"
/>
<
TextBlock
Text
=
"{Binding LastName}"
Grid.Row
=
"2"
HorizontalAlignment
=
"Center"
Foreground
=
"#FF767676"
/>
</
Grid
>
</
DataTemplate
>
</
ListBox.ItemTemplate
>
</
ListBox
>
</
DockPanel
>
</
Border
>
<
TextBox
Height
=
"35"
Name
=
"TextBox1"
Style
=
"{StaticResource textBoxStyle}"
AllowDrop
=
"True"
Width
=
"225"
Grid.Column
=
"2"
Margin
=
"24,204,13,8"
telerikDragandDrop:TextBoxDragDropBehaviour.IsEnabled
=
"True"
telerik:TouchManager.IsTouchHitTestVisible
=
"True"
/>
</
Grid
>
</
UserControl
>
public class TextBoxDragDropBehaviour
{
private TextBox _associatedObject;
/// <
summary
>
/// AssociatedObject Property
/// </
summary
>
public TextBox AssociatedObject
{
get
{
return _associatedObject;
}
set
{
_associatedObject = value;
}
}
private static Dictionary<
TextBox
, TextBoxDragDropBehaviour> instances;
static TextBoxDragDropBehaviour()
{
instances = new Dictionary<
TextBox
, TextBoxDragDropBehaviour>();
}
public static bool GetIsEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsEnabledProperty);
}
public static void SetIsEnabled(DependencyObject obj, bool value)
{
TextBoxDragDropBehaviour behavior = GetAttachedBehavior(obj as TextBox );
behavior.AssociatedObject = obj as TextBox;
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(TextBoxDragDropBehaviour),
new PropertyMetadata(new PropertyChangedCallback(OnIsEnabledPropertyChanged)));
public static void OnIsEnabledPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
SetIsEnabled(dependencyObject, (bool)e.NewValue);
}
private static TextBoxDragDropBehaviour GetAttachedBehavior(TextBox listBox)
{
if (!instances.ContainsKey(listBox))
{
instances[listBox] = new TextBoxDragDropBehaviour();
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 listBoxItem = e.OriginalSource as System.Windows.Controls.ListBoxItem ?? (e.OriginalSource as FrameworkElement).ParentOfType<
System.Windows.Controls.ListBoxItem
>();
var textBox = sender as System.Windows.Controls.TextBox;
if (textBox != null)
{
var item = listBoxItem != null ? listBoxItem.DataContext : textBox.Text;
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, DragDropCompletedEventArgs e)
{
var draggedItem = DragDropPayloadManager.GetDataFromObject(e.Data, "DraggedData");
if (e.Effects != DragDropEffects.None)
{
//var collection = (sender as TextBox).Text;
//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;
var itemsType = (this.AssociatedObject.Text);
//if (details == null || draggedItem == null || draggedItem.GetType() != itemsType)
//{
// return;
//}
if (e.Effects != DragDropEffects.None)
{
var collection = (sender as TextBox);
if (collection != null) collection.Text = collection.Text;
}
}
private void OnDragOver(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
{
//var draggedItem = DragDropPayloadManager.GetDataFromObject(e.Data, "DraggedData");
//var itemsType = (this.AssociatedObject.ItemsSource 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 = Controls.DropPosition.Inside;
e.Handled = true;
}
}