ListBoxItem Content Cast Impossible

90 Views Asked by At

I have a problem in c# using.Net 4.5.

When I used .Net 3.5 I didn't have any problem but since I changed my project to .Net 4.5, when I do my drag&drop on listBoxItem between two listBox I have an exception :

"Impossible to cast an object of type MS.Internal.Named.Object in type..."

Any ideas regarding the problem ?

(Sorry for my english, I'm French ^^)

EDIT : Yes, and I loose some data when I use "as...". Finaly I redefined my functions for Drag&Frop and the problem is resolved.

Here is the code for the Drag&Drop if someone is interested :) :

region Drag&Drop

    /// <summary>
    /// Define the action when the left mouse button is pressed while the mouse pointer is over this element. Permit to get the shape selected by the clic
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void ListBoxShapeCluster_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        System.Windows.Controls.ListBox parent = (System.Windows.Controls.ListBox)sender;
        dragSource = parent;
        object data = GetDataFromListBox(dragSource, e.GetPosition(parent));

        if (data != null)
        {
            DragDrop.DoDragDrop(parent, data, System.Windows.DragDropEffects.Move);
        }

    }
    /// <summary>
    /// Define the action of the drag enter
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void ListBoxShapeCluster_DragEnter(object sender, System.Windows.DragEventArgs e)
    {
        object data = e.Data.GetData(typeof(Retro.Model.core.Shape));
        if (data != null)
        {
            AllShapesOfCurrentCluster.Remove((Retro.Model.core.Shape)data);
        }

    }
    /// <summary>
    /// Define the action for drop a shape in a new cluster
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void ListBoxShapeCluster_Drop(object sender, System.Windows.DragEventArgs e)
    {

        System.Windows.Controls.ListBox parent = (System.Windows.Controls.ListBox)sender;
        object data = e.Data.GetData(typeof(Retro.Model.core.Shape));
        if (data != null)
        {
            AllShapesOfCurrentCluster.Add((Retro.Model.core.Shape)data);
        }
        
    }



    #endregion
1

There are 1 best solutions below

1
On

Did you try

 _dragged.Content AS Retro.Model.core.Shape