Reputation: 1017
I am building a custom control that will allow me to drag and drop between controls. I am following This that gets me all the way there until I want to allow for multiple types. So I may want to use this in a customers form and drag between two customer ListViews
, or an Employee
form and drag between two employee forms.
For some reason I cannot cast as Type
. Please explain where I am going wrong and why. Thank you!
private void OnMouseMove(object sender, MouseEventArgs e)
{
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)
{
if (ListView != null)
{
ListViewItem listViewItem =
FindAnchestor<ListViewItem>((DependencyObject) e.OriginalSource);
Type itemType = ListView.ItemContainerGenerator.ItemFromContainer(listViewItem).GetType();
var item = (itemType)ListView.ItemContainerGenerator.ItemFromContainer(listViewItem);
}
}
}
Upvotes: 1
Views: 352
Reputation: 172230
Please explain where I am going wrong and why.
You are trying to statically cast to a dynamically determined type.
The bad news: That doesn't work. Static types must be known at compile time.
The good news: There's absolutely no need to do this: For your purposes, storing the reference in a generic object variable suffices:
object item = ListView.ItemContainerGenerator.ItemFromContainer(listViewItem);
Upvotes: 1