前台
<UserControl xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" x:Class="ComputerDropDragControl.HandCar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Width="200" Height="1010">
<StackPanel>
<TextBlock Text="等待上架的设备" Margin="10,10,0,0" Visibility="Collapsed"></TextBlock>
<toolkit:ListBoxDragDropTarget x:Name="handCarPanel" AllowDrop="True" HorizontalAlignment="Left"
ItemDragStarting="handCar_ItemDragStarting"
Drop="handCar_Drop" ItemDroppedOnTarget="handCar_ItemDroppedOnTarget">
<ListBox Width="180" Height="880" x:Name="handCar" Margin="10,10,0,0">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel></StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</toolkit:ListBoxDragDropTarget>
</StackPanel>
</Grid>
</UserControl>
后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
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;
namespace ComputerDropDragControl
{
/// <summary>
/// “手推车”界面
/// 创建人:吴兆娟
/// 创建时间:2011-11-3
/// </summary>
public partial class HandCar : UserControl
{
#region <<页面加载>>
/// <summary>
/// 页面加载
/// </summary>
public HandCar()
{
InitializeComponent();
//加载数据
LoadData();
}
#endregion
#region <<控件事件>>
/// <summary>
/// “开始拖”
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void handCar_ItemDragStarting(object sender, ItemDragEventArgs e)
{
StaticClass.FromList = true;
StaticClass.FromHandCar = true;
StaticClass.DragPanelList = (ListBoxDragDropTarget)sender;
ListBox list = (ListBox)this.handCarPanel.Content;
ListBoxItem selectItem = (ListBoxItem)list.SelectedItem;
int index = (int)list.SelectedIndex;
SelectListItem = selectItem;
SelectIndex = index;
}
/// <summary>
/// “放”
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void handCar_Drop(object sender, Microsoft.Windows.DragEventArgs e)
{
if (!StaticClass.FromList)
{
StaticClass.DropInHandCar = true;
StaticClass.DrogPanelList = (ListBoxDragDropTarget)sender;
//“拖”
StackPanel stackDrag = (StackPanel)StaticClass.DragPanel.Content;
ComputerUnit Cu = (ComputerUnit)stackDrag.Children[3];
//“放”
ListBoxDragDropTarget DropPanel = (ListBoxDragDropTarget)sender;
ListBox lbDrop = (ListBox)DropPanel.Content;
ListBoxItem lbi = new ListBoxItem();
lbi.DataContext = Cu.ReturnNodeFrameView;
lbi.Content = Cu.ReturnNodeFrameView.NodeName;
//lbi.Name = "LbiName" + (StaticClass.HandCarList.Count + 1).ToString();
lbDrop.Items.Add(lbi);
}
else //“来自本身”
{
StaticClass.FromList = false;
}
//StaticClass.HandCarList.Add(lbi);//??
}
/// <summary>
/// “放好在目标后”
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void handCar_ItemDroppedOnTarget(object sender, ItemDragEventArgs e)
{
if (StaticClass.DropPanel != null)
{
if (StaticClass.DropPanel.AllowDrop)
{
if (!StaticClass.ExistPlace)
{
((StackPanel)StaticClass.DropPanel.Content).Children.RemoveAt(3);
StaticClass.DropPanel.AllowDrop = true;
ListBox list = (ListBox)this.handCarPanel.Content;
list.Items.Insert(SelectIndex, SelectListItem);
}
}
}
}
#endregion
#region <<辅助方法>>
/// <summary>
/// “加载数据”
/// </summary>
private void LoadData()
{
//if (StaticClass.HandCarList != null)
//{
// foreach (ListBoxItem item in StaticClass.HandCarList)
// {
// ListBoxItem lbi = new ListBoxItem();
// lbi.Content = item.Content;
// lbi.DataContext = item.DataContext;
// lbi.Name = item.Name;
// this.handCar.Items.Add(lbi);
// }
//}
}
#endregion
public ListBoxItem SelectListItem { get; set; }
public int SelectIndex { get; set; }
}
}