This question is locked. New answers and comments are not allowed.
I cant seem to get my head around dropping data from a gridview(students) to certain nodes in a treelistview(markers). Could I get any help?
using
System;
using
System.Collections;
using
System.Collections.Generic;
using
System.Collections.ObjectModel;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Data;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
using
Telerik.Windows;
using
Telerik.Windows.Controls;
using
Telerik.Windows.Controls.DragDrop;
using
Telerik.Windows.Controls.TreeView;
using
InfoSys342.UI.ViewModels;
namespace
InfoSys342.UI
{
public
partial
class
MarkersByStudentView : UserControl
{
private
MarkersViewModel viewModel;
private
StudentsViewModel viewModel2;
private
ObservableCollection<String> list {
get
;
set
; }
StudentMark ss =
new
StudentMark();
public
MarkersByStudentView(MainViewModel mainViewModel)
{
InitializeComponent();
this
.viewModel2 =
new
MarkersViewModel(mainViewModel);
this
.mlist.DataContext =
this
.viewModel2;
this
.viewModel =
new
StudentsViewModel(mainViewModel);
this
.sList.DataContext =
this
.viewModel;
RadDragAndDropManager.AddDragQueryHandler(
this
.mlist,
new
EventHandler<DragDropQueryEventArgs>(OnDragQuery));
RadDragAndDropManager.AddDragInfoHandler(
this
.mlist,
new
EventHandler<DragDropEventArgs>(OnDragInfo));
RadDragAndDropManager.AddDropQueryHandler(
this
.sList, OnDropQuery);
RadDragAndDropManager.AddDropInfoHandler(
this
.sList, OnDropInfo);
}
private
void
OnDragQuery(
object
sender, DragDropQueryEventArgs e)
{
//We are asked here if the item should be dragged:
if
(e.Options.Status == DragStatus.DragQuery)
{
//The answer is yes, but we can get e.Options.Source and
//check the item for something and only then allow it.
e.QueryResult =
true
;
//Generally it is a good idea to handle routed events if
//we are taking some action.
e.Handled =
true
;
//We want to make sure that the payload is something maningful that can
//be used by the destination side. We could simply add the dragged item (or items)
//or we could create a custom class that will provide more information in
//a complex app.
//Here we get the ViewModel iitem. We could get the selected items of a cotnrol
//for multiple items DragDrop.
RadTreeListView treeView = e.Options.Source
as
RadTreeListView;
if
(treeView !=
null
&& treeView.SelectedItem !=
null
)
{
e.Options.Payload = treeView.SelectedItem;
}
else
{
e.Options.Payload = (e.Source
as
FrameworkElement).DataContext;
}
//We can decide to add a drag cue here
//to help the user. We can add any ContentControl as a
//drag cue but we can also generate an arrow because it is easier:
e.Options.ArrowCue = RadDragAndDropManager.GenerateArrowCue();
}
}
private
void
OnDragInfo(
object
sender, DragDropEventArgs e){
//We want to remove the item on a successful drop. But we have added
//this event handler to the root visual, we have to then check the
//drag source here. Alternatively we can add separates handlers to
//the different controls.
if
(e.Options.Status == DragStatus.DragComplete)
{
IList itemsSource =
null
;
if
(e.Options.Destination
is
Telerik.Windows.Controls.RadTreeListView)
{
itemsSource = (e.Options.Destination
as
Telerik.Windows.Controls.RadTreeListView).ItemsSource
as
IList;
}
else
{
itemsSource = (e.Options.Destination
as
RadTreeListView).ItemsSource
as
IList;
}
// itemsSource.Remove(e.Options.Payload);
}
}
private
void
OnDropQuery(
object
sender, DragDropQueryEventArgs e)
{
//Again, we handle the drop query at a root leve, but we could
//add the handler to a specific control if we want more granularity.
if
(e.Options.Status == DragStatus.DropDestinationQuery)
{
//We would only check that the source and the destination are
//different
e.QueryResult = e.Options.Destination != e.Options.Source;
e.Handled =
true
;
}
}
private
void
OnDropInfo(
object
sender, DragDropEventArgs e)
{
//Here we can change the drag cue to let the user know what
//action is possible. We could change the background of the
//arrow if it is in use.
if
(e.Options.Status == DragStatus.DropPossible)
{
//Drop is possible, change the content of the DragCue or
//notify the user that a drag is possible.
}
if
(e.Options.Status == DragStatus.DropImpossible)
{
//Drop is not possible. Here generally we will revert the changes
//done in DropPossible.
}
if
(e.Options.Status == DragStatus.DropComplete)
{
//Here we can generally check the payload and add handle the case
//when it is an IEnumerable (when multiple items are dragged).
//The paylaod could also be a custom class that contains mroe info
//for the DragDrop. HEre we know that it always is an item that we can
//display. We just add the item here, we do not interact with the
//drag source. Removing the item (if at all removed should happen OnDragIndo
//so that there will be separation of handling the DragDrop, which is more flexible.
IList itemsSource =
null
;
if
(e.Options.Destination
is
Telerik.Windows.Controls.RadTreeListView)
{
itemsSource = (e.Options.Destination
as
Telerik.Windows.Controls.RadTreeListView).ItemsSource
as
IList;
}
else
{
itemsSource = (e.Options.Destination
as
RadTreeListView).ItemsSource
as
IList;
}
itemsSource.Add(e.Options.Payload);
}
}
}
}
XAML:
<
UserControl
x:Class
=
"InfoSys342.UI.MarkersByStudentView"
xmlns:telerik
=
"http://schemas.telerik.com/2008/xaml/presentation"
xmlns:telerikNavigation
=
"clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"
xmlns:controls
=
"clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:grid
=
"http://schemas.telerik.com/2008/xaml/presentation"
xmlns:gridView
=
"clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
xmlns:gridViewElements
=
"clr-namespace:Telerik.Windows.Controls.GridView;assembly=Telerik.Windows.Controls.GridView"
xmlns:local
=
"clr-namespace:InfoSys342.UI"
xmlns:dragDrop
=
"clr-namespace:Telerik.Windows.Controls.DragDrop;assembly=Telerik.Windows.Controls"
xmlns:telerikDragDrop
=
"clr-namespace:Telerik.Windows.Controls.DragDrop;assembly=Telerik.Windows.Controls"
mc:Ignorable
=
"d"
d:DesignHeight
=
"768"
d:DesignWidth
=
"1024"
>
<
UserControl.Resources
>
<
Style
TargetType
=
"TextBlock"
x:Key
=
"LabelStyle"
>
<
Setter
Property
=
"Margin"
Value
=
"0,0,3,0"
/>
</
Style
>
<
Style
TargetType
=
"TextBlock"
x:Key
=
"ValueStyle"
>
<
Setter
Property
=
"Margin"
Value
=
"0,0,3,0"
/>
<
Setter
Property
=
"FontWeight"
Value
=
"Bold"
/>
</
Style
>
<
local:PercentageToColourConverter
x:Key
=
"ColourConverter"
/>
</
UserControl.Resources
>
<
Grid
>
<
Grid.ColumnDefinitions
>
<
ColumnDefinition
Width
=
"*"
/>
<
ColumnDefinition
Width
=
"*"
/>
</
Grid.ColumnDefinitions
>
<
Border
CornerRadius
=
"3"
Padding
=
"3"
Background
=
"White"
BorderBrush
=
"#888888"
BorderThickness
=
"1"
Margin
=
"0,0,10,0"
>
<
grid:RadDockPanel
LastChildFill
=
"True"
>
<
gridView:RadGridView
AutoGenerateColumns
=
"True"
x:Name
=
"mlist"
dragDrop:RadDragAndDropManager.AllowDrop
=
"True"
ItemsSource
=
"{Binding Students}"
CanUserDeleteRows
=
"False"
CanUserInsertRows
=
"False"
IsReadOnly
=
"True"
RowIndicatorVisibility
=
"Collapsed"
ShowInsertRow
=
"False"
SelectionMode
=
"Single"
SelectedItem
=
"{Binding CurrentStudent, Mode=TwoWay}"
ShowGroupPanel
=
"False"
>
<
gridView:RadGridView.RowStyle
>
<
Style
TargetType
=
"gridViewElements:GridViewRow"
>
<
Setter
Property
=
"dragDrop:RadDragAndDropManager.AllowDrag"
Value
=
"True"
/>
</
Style
>
</
gridView:RadGridView.RowStyle
>
</
gridView:RadGridView
>
</
grid:RadDockPanel
>
</
Border
>
<
controls:GridSplitter
HorizontalAlignment
=
"Right"
VerticalAlignment
=
"Stretch"
Width
=
"10"
Margin
=
"0,3"
Background
=
"Transparent"
/>
<
Border
Grid.Column
=
"1"
CornerRadius
=
"3"
Padding
=
"3"
Background
=
"White"
BorderBrush
=
"#888888"
BorderThickness
=
"1"
>
<
grid:RadDockPanel
LastChildFill
=
"True"
>
<
gridView:RadTreeListView
x:Name
=
"sList"
dragDrop:RadDragAndDropManager.AllowDrop
=
"True"
ItemsSource
=
"{Binding Staff}"
CanUserDeleteRows
=
"False"
CanUserInsertRows
=
"True"
IsReadOnly
=
"False"
RowIndicatorVisibility
=
"Collapsed"
ShowInsertRow
=
"False"
SelectionMode
=
"Single"
SelectedItem
=
"{Binding CurrentStudent, Mode=TwoWay}"
ShowGroupPanel
=
"False"
AllowDrop
=
"True"
>
<
gridView:RadTreeListView.ItemContainerStyle
>
<
Style
TargetType
=
"gridView:RadTreeListView"
>
<
Setter
Property
=
"dragDrop:RadDragAndDropManager.AllowDrop"
Value
=
"True"
/>
<
Setter
Property
=
"Foreground"
Value
=
"Red"
/>
</
Style
>
</
gridView:RadTreeListView.ItemContainerStyle
>
</
gridView:RadTreeListView
>
</
grid:RadDockPanel
>
</
Border
>
</
Grid
>
</
UserControl
>