public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 {
     try
     {
         Vertex    v = value as Vertex;
         UIElement e = ViewModelVertexEdge.findVertexPresenter(v);
         if (e == null)
         {
             return(Binding.DoNothing);
         }
         return(e);
     }
     catch (InvalidCastException)
     {
         return(Binding.DoNothing);
     }
 }
        public MainWindow()
        {
            InitializeComponent();
            viewModel   = new ViewModelVertexEdge();
            DataContext = viewModel;

            ViewModelVertexEdge.graphControl = monitor;
            FollowupWindow             = new Dijkstra_s_Algorithm_Followup_Window();
            FollowupWindow.viewModel   = viewModel;
            FollowupWindow.DataContext = viewModel;

            UIElement cvs = monitor as UIElement;

            // These lambda functions are for draging Vertex nodes around.
            moveStart = (varMoved, args) =>
            {
                var element = varMoved as UIElement;
                dragStart = args.GetPosition(element);
                element.CaptureMouse();
            };
            moveEnd = (varMoved, args) =>
            {
                var element = varMoved as UIElement;
                dragStart = null;
                element.ReleaseMouseCapture();
            };
            moving = (varMoved, args) =>
            {
                if (dragStart != null && args.LeftButton == MouseButtonState.Pressed)
                {
                    var element = (UIElement)varMoved;
                    var p2      = args.GetPosition(cvs);
                    if (p2.X - dragStart.Value.X > 0)
                    {
                        Canvas.SetLeft(element, p2.X - dragStart.Value.X);
                    }
                    else
                    {
                        Canvas.SetLeft(element, 0);
                    }
                    if (p2.Y - dragStart.Value.Y > 0)
                    {
                        Canvas.SetTop(element, p2.Y - dragStart.Value.Y);
                    }
                    else
                    {
                        Canvas.SetTop(element, 0);
                    }
                }
            };

            // This function is for selecting elements on the screen.
            selectPresenter = (varMoved, args) =>
            {
                var presenter = varMoved as ContentPresenter;

                switch (viewModel.CurrentStatus)
                {
                case SelectStatus.SelectAnElement:
                    if (presenter.Content is Vertex)
                    {
                        CancelSelectionAndResetStatus();
                        viewModel.SelectVertex(presenter.Content as Vertex);
                        args.Handled = true;
                    }
                    if (presenter.Content is EdgeViewModelClass)
                    {
                        CancelSelectionAndResetStatus();
                        viewModel.SelectEdge((presenter.Content as EdgeViewModelClass).edge);
                        args.Handled = true;
                    }
                    break;

                case SelectStatus.EdgeBuilding:
                    if (presenter.Content is Vertex && presenter.Content != viewModel.CurrentVertexSelected)
                    {
                        Edge e = AddEdge(viewModel.CurrentVertexSelected, presenter.Content as Vertex);
                        CancelSelectionAndResetStatus();
                        if (e != null)
                        {
                            viewModel.SelectEdge(e);
                        }
                        args.Handled = true;
                    }
                    else
                    {
                        CancelSelectionAndResetStatus();
                    }
                    break;

                case SelectStatus.SelectAStartingVertex:
                    if (presenter.Content is Vertex)
                    {
                        CancelSelectionAndResetStatus();
                        viewModel.SelectStartVertex(presenter.Content as Vertex);
                        args.Handled = true;
                    }
                    else
                    {
                        CancelSelectionAndResetStatus();
                    }
                    break;

                case SelectStatus.SelectAnEndVertex:
                    if (presenter.Content is Vertex)
                    {
                        CancelSelectionAndResetStatus();
                        viewModel.SelectEndVertex(presenter.Content as Vertex);
                        args.Handled = true;
                    }
                    else
                    {
                        CancelSelectionAndResetStatus();
                    }
                    break;

                default:
                    CancelSelectionAndResetStatus();
                    break;
                }
            };
        }