0
我目前試圖實現拖放我的列表框。我需要處理被拖動的元素。如何正確使用Data.GetData()父類
每個拖動對象中的一個繼承卡利科技的屏幕和我的BaseViewModel
例如
BaseViewModel : Screen
ListBoxItem1 DataContext = PersonViewModel : BaseViewModel
ListBoxItem2 DataContext = BusinessViewModel : BaseViewModel
private void ListBoxItem_DropEvent(object sender, DragEventArgs e)
{
if (sender is ListBoxItem)
{
var source = e.Data.GetData(typeof(PersonViewModel)) as PersonViewModel ;
var target = ((ListBoxItem)(sender)).DataContext as BusinessViewModel;
int sourceIndex = Items.Items.IndexOf(source);
int targetIndex = Items.Items.IndexOf(target);
Move(source, sourceIndex, targetIndex);
}
}
所以,我目前所面對的問題是,我不希望指定PersonViewModel/BusinessViewModel我試圖通過typeof使用繼承值希望BaseViewModel或屏幕。我可以得到一些提示或建議
我試過下面的解決方案,但他們返回空。
var test = e.Data.GetData(typeof(BaseViewModel)) as BaseViewModel;
var test1 = e.Data.GetData(typeof(Screen)) as Screen;
上鼠標事件
private void PreviewMouseMove(object sender, MouseEventArgs e)
{
Point point = e.GetPosition(null);
Vector diff = _dragStartPoint - point;
if (e.LeftButton == MouseButtonState.Pressed &&
(Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
var lb = sender as ListBox;
var lbi = FindVisualParent<ListBoxItem>(((DependencyObject)e.OriginalSource));
if (lbi != null)
{
DragDrop.DoDragDrop(lbi, lbi.DataContext, DragDropEffects.Move);
}
}
}