2
我正在使用拖放在我的程序中,它運作良好。但是我正在處理一個列表框中的單詞列表,當我選擇一個單詞並將其拖動到另一個列表框時,用戶不再知道他選擇了哪個單詞,因爲第一個列表框中的「視覺」選項不會不會出現。有誰知道我如何看到列表框中的選定項目?在實現拖放操作之前,選擇單詞時所選單詞會變成另一種顏色,但是當我添加拖放操作時,我再也看不到它了。誰能幫我?wpf拖放視覺選擇項目
http://img196.imageshack.us/img196/8408/imgmt.jpg
private void lstAlleTabellen_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
startpoint = e.GetPosition(null);
}
private void lstAlleTabellen_MouseMove(object sender, MouseEventArgs e)
{
// Get the current mouse position
System.Windows.Point mousePos = e.GetPosition(null);
Vector diff = startpoint - mousePos;
if (e.LeftButton == MouseButtonState.Pressed &&
Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
{
// Get the dragged ListViewItem
System.Windows.Controls.ListBox listAlle = sender as System.Windows.Controls.ListBox;
ListBoxItem listItem =
FindAnchestor<ListBoxItem>((DependencyObject)e.OriginalSource);
Tabel tabel=new Tabel();
try
{
// Find the data behind the ListViewItem
tabel = (Tabel)listAlle.ItemContainerGenerator.
ItemFromContainer(listItem);
// Initialize the drag & drop operation
DataObject dragData = new DataObject("myFormat", tabel);
DragDrop.DoDragDrop(listItem, dragData, DragDropEffects.Move);
}
catch
{
}
}
}
private static T FindAnchestor<T>(DependencyObject current) where T : DependencyObject
{
do
{
if (current is T)
{
return (T)current;
}
current = VisualTreeHelper.GetParent(current);
}
while (current != null);
return null;
}
private void lstGekozenTabellen_DragEnter(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent("myFormat") ||sender == e.Source)
{
e.Effects = DragDropEffects.None;
}
}
private void lstGekozenTabellen_Drop(object sender, DragEventArgs e)
{
try
{
if (e.Data.GetDataPresent("myFormat"))
{
Tabel tabel = e.Data.GetData("myFormat") as Tabel;
System.Windows.Controls.ListBox listGekozen = sender as System.Windows.Controls.ListBox;
listGekozen.DisplayMemberPath = "naam";
listGekozen.SelectedValuePath = "naam";
listGekozen.Items.Add(tabel);
lTabellen.Remove(tabel);
lstAlleTabellen.ItemsSource = null;
lstAlleTabellen.Items.Clear();
lstAlleTabellen.ItemsSource = lTabellen;
}
}
catch { }
}
}