前半厂
<UserControl xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" x:Class="ComputerDropDragControl.SearchPanel"
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" >
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10,10,0,0">
<TextBlock Text="设备名称:" VerticalAlignment="Center"></TextBlock>
<TextBox Width="125" Height="20" x:Name="txtNodeName"></TextBox>
</StackPanel>
<Button x:Name="btnSearch" Grid.Row="1" Width="50" Height="25" Content="查询" HorizontalAlignment="Right"
Click="btnSearch_Click" Margin="0,0,10,0"></Button>
<StackPanel Grid.Row="2">
<toolkit:ListBoxDragDropTarget x:Name="listDrag" AllowDrop="False" HorizontalAlignment="Left" Margin="10,0,0,0"
ItemDragStarting="listDrag_ItemDragStarting" ItemDroppedOnTarget="listDrag_ItemDroppedOnTarget">
<ListBox Width="180" Height="845" x:Name="listComputer">
</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;
using System.Collections.ObjectModel;
namespace ComputerDropDragControl
{
/// <summary>
/// “查询界面”
/// 创建人:吴兆娟
/// 创建时间:2011-11-1
/// </summary>
public partial class SearchPanel : UserControl
{
#region <<页面加载>>
/// <summary>
/// 页面加载
/// </summary>
public SearchPanel()
{
InitializeComponent();
//加载数据
LoadData();
}
#endregion
#region <<控件事件>>
#region <<拖拽相关事件>>
/// <summary>
/// “拖”事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listDrag_ItemDragStarting(object sender, ItemDragEventArgs e)
{
StaticClass.FromList = true;
StaticClass.DragPanelList = (ListBoxDragDropTarget)sender;
ListBox list = (ListBox)this.listDrag.Content;
ListBoxItem selectItem = (ListBoxItem)list.SelectedItem;
int index = (int)list.SelectedIndex;
SelectListItem = selectItem;
SelectIndex = index;
}
private void listDrag_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.listDrag.Content;
list.Items.Insert(SelectIndex, SelectListItem);
}
}
}
}
#endregion
/// <summary>
/// “查询”按钮点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
List<NodeFrameView> result = nList.ToList<NodeFrameView>();
if (!string.IsNullOrEmpty(this.txtNodeName.Text))
{
result = (from nodeFrameView in nList
where nodeFrameView.NodeName.IndexOf(this.txtNodeName.Text.Trim()) > 0
select nodeFrameView).ToList<NodeFrameView>();
}
listComputer.Items.Clear();
foreach (NodeFrameView item in result)
{
ListBoxItem li = new ListBoxItem();
li.Content = item.NodeName;
li.DataContext = item;
listComputer.Items.Add(li);
}
}
#endregion
#region <<辅助方法>>
public ObservableCollection<NodeFrameView> nList;
private ObservableCollection<NodeFrameView> LoadData()
{
return nList = new ObservableCollection<NodeFrameView>
{
new NodeFrameView("n1","List设备一",2),
new NodeFrameView("n2","List设备二",3),
new NodeFrameView("n3","List设备三",3),
new NodeFrameView("n4","List设备五",3),
new NodeFrameView("n5","List设备六",2),
new NodeFrameView("n6","List设备七",1),
new NodeFrameView("n7","List设备八",2)
};
}
#endregion
public ListBoxItem SelectListItem { get; set; }
public int SelectIndex { get; set; }
}
}