using System.Windows; |
using System.Windows.Controls; |
using System.Windows.Media; |
using System.Windows.Shapes; |
using System.Windows.Navigation; |
using Telerik.Windows.Controls.DragDrop; |
using Telerik.Windows; |
|
namespace EnterpriseDashboard.SL3.Views |
{ |
public partial class DragTest : Page |
{ |
private ObservableCollection<Summary> summaries = GenerateSummaries(); |
|
public DragTest() |
{ |
InitializeComponent(); |
//SummariesListBox.ItemsSource = summaries; |
|
dragGrid.AddHandler(RadDragAndDropManager.DragQueryEvent, new EventHandler<DragDropQueryEventArgs>(OnDragQuery), true); |
dragGrid.AddHandler(RadDragAndDropManager.DropQueryEvent, new EventHandler<DragDropQueryEventArgs>(OnDropQuery), true); |
dragGrid.AddHandler(RadDragAndDropManager.DropInfoEvent, new EventHandler<DragDropEventArgs>(OnDropInfo), true); |
dragGrid.AddHandler(RadDragAndDropManager.DragInfoEvent, new EventHandler<DragDropEventArgs>(OnDragInfo), true); |
|
Loaded += new RoutedEventHandler(Page_Loaded); |
} |
void Page_Loaded(object sender, RoutedEventArgs e) |
{ |
int col = 0; |
int row = 0; |
CreateRowWithDropTargets(row); |
foreach (Summary summary in summaries) |
{ |
if (col > 2) |
{ |
col = 0; //End of row, so reset column index and create new row |
row += 1; |
CreateRowWithDropTargets(row); |
} |
//Create a border with a textblock child -- this is the item to be dragged around. |
var border = new Border() |
{ |
BorderBrush = new SolidColorBrush(Colors.Black), |
Background = new SolidColorBrush(Colors.White), |
BorderThickness = new Thickness(1), |
Margin = new Thickness(15) |
}; |
var txtBlock = new TextBlock() { Text = summary.Title }; |
border.Child = txtBlock; |
RadDragAndDropManager.SetAllowDrag(border, true); |
|
//Add object to grid and place it in correct column and row |
dragGrid.Children.Add(border); |
Grid.SetColumn(border, col); |
Grid.SetRow(border, row); |
col += 1; |
} |
} |
private void CreateRowWithDropTargets(int row) |
{ |
dragGrid.RowDefinitions.Add(new RowDefinition()); |
for (int i = 0; i < 3; i++) |
{ |
var dropRectangle = new Rectangle() { Fill = new SolidColorBrush(Colors.Transparent), Stroke = new SolidColorBrush(Colors.LightGray) }; |
RadDragAndDropManager.SetAllowDrop(dropRectangle, true); |
dragGrid.Children.Add(dropRectangle); |
Grid.SetColumn(dropRectangle, i); |
Grid.SetRow(dropRectangle, row); |
} |
} |
|
private void OnDragQuery(object sender, DragDropQueryEventArgs e) |
{ |
if (e.Options.Status == DragStatus.DragQuery) |
{ |
e.QueryResult = true; |
|
//The DragAndDropmanager will detach the element and drag it around the screen. |
var source = e.Options.Source as Border; |
source.Visibility = Visibility.Collapsed; |
|
e.Options.Payload = source; |
e.Options.DragCue = new Border() { Background = new SolidColorBrush(Colors.Gray), Width = 100, Height = 100, BorderBrush = new SolidColorBrush(Colors.Gray)}; |
|
e.Handled = true; |
} |
|
if (e.Options.Status == DragStatus.DropSourceQuery) |
{ |
e.QueryResult = true; |
} |
} |
|
private void OnDropQuery(object sender, DragDropQueryEventArgs e) |
{ |
if (e.Options.Status == DragStatus.DropDestinationQuery) |
{ |
e.QueryResult = true; |
} |
} |
|
private void OnDragInfo(object sender, DragDropEventArgs e) |
{ |
if (e.Options.Status == DragStatus.DragCancel) |
{ |
e.Options.Source.Visibility = Visibility.Visible; |
} |
} |
|
private void OnDropInfo(object sender, DragDropEventArgs e) |
{ |
if (e.Options.Status == DragStatus.DropComplete) |
{ |
Grid.SetColumn(e.Options.Source, Grid.GetColumn(e.Options.Destination)); |
Grid.SetRow(e.Options.Source, Grid.GetRow(e.Options.Destination)); |
|
e.Options.Source.Visibility = Visibility.Visible; |
|
e.Handled = true; |
} |
} |
|
|
public static ObservableCollection<Summary> GenerateSummaries() |
{ |
return new ObservableCollection<Summary>() |
{ |
new Summary(){Title = "Summary 1"}, |
new Summary(){Title = "Summary 2"}, |
new Summary(){Title = "Summary 3"}, |
new Summary() {Title = "Summary 4"} |
}; |
} |
|
// Executes when the user navigates to this page. |
protected override void OnNavigatedTo(NavigationEventArgs e) |
{ |
} |
|
public class Summary |
{ |
public string Title { get; set; } |
} |
} |
} |