简体   繁体   中英

Creating generic object on runtime

I'm making an application with drag and drop and what i basically want is to create a new object when i drag the object from a list and into the "Main area". I Have an abstract class

public abstract class SymbolModel

And 2 (need a lot more) classes inhereting from it

public class ValveModel : SymbolModel
public class LightBulbModel : SymbolModel

When i drag and drop it shows up but when i drag multiple they're all the same. I've made a click function which hits all of them where i only want to do it on the one clicked. My dragfrom method looks like this:

private void UIElement_OnMouseMove(object sender, MouseEventArgs e)
    {
       TextBlock txtBlock = sender as TextBlock;
        if (txtBlock == null) return;
        if (!(txtBlock.DataContext is SymbolModel)) return;
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            DataObject data = new DataObject();
            data.SetData("Object", (SymbolModel) txtBlock.DataContext);
            DragDrop.DoDragDrop(this, data, DragDropEffects.Copy | DragDropEffects.Move);
        }
    }

My drop method looks like this:

 private void SymbolView_OnDrop(object sender, DragEventArgs e)
    {
        Point pos = e.GetPosition(SymbolViewControl);
        Console.WriteLine(e.Data.GetData("Object").ToString());
        SymbolModel obj = (SymbolModel) e.Data.GetData("Object");
        obj.CanvasTopImage = pos.Y;
        obj.CanvasLeftImage = pos.X;
        _symbolViewModel.Symbols.Add(obj);
    }

And my click method is here:

 private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        if (!(sender is Button btn)) return;
        if (!(btn.DataContext is SymbolModel)) return;
        SymbolModel symbol = (SymbolModel) btn.DataContext;
        foreach (SymbolModel sym in _symbolViewModel.Symbols)
        {
            Console.WriteLine(sym.Id);
        }

        if (symbol.ImageName.Equals("valve_green.png"))
        {
            symbol.ImageName = "valve_red.png";
        }
        else
        {
            symbol.ImageName = "valve_green.png";
        }
    }

What i want to happen here is that when it's dropped it becomes a new entity independent of the others.

Hope this makes sense! Thank you!

So i got it to work by changing my drop method to:

private void SymbolView_OnDrop(object sender, DragEventArgs e)
    {
        Point pos = e.GetPosition(SymbolViewControl);
        Console.WriteLine(e.Data.GetData("Object").ToString());
        SymbolModel obj = (SymbolModel) e.Data.GetData("Object");
        Type t = obj.GetType();
        var symbol = (SymbolModel)Activator.CreateInstance(t);
        symbol.CanvasTopImage = pos.Y;
        symbol.CanvasLeftImage = pos.X;
        _symbolViewModel.Symbols.Add(symbol);
    }

Its seems that the Activator was what i needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM