Click here to Skip to main content
13,900,479 members
Click here to Skip to main content


557 bookmarked
Posted 24 Feb 2008
Licenced CPOL

WPF Diagram Designer - Part 3

, 29 Feb 2008
Connecting items
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;

namespace DiagramDesigner
    // Represents a selectable item in the Toolbox/>.
    public class ToolboxItem : ContentControl
        // caches the start point of the drag operation
        private Point? dragStartPoint = null;

        static ToolboxItem()
            // set the key to reference the style for this control
                typeof(ToolboxItem), new FrameworkPropertyMetadata(typeof(ToolboxItem)));

        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
            this.dragStartPoint = new Point?(e.GetPosition(this));

        protected override void OnMouseMove(MouseEventArgs e)
            if (e.LeftButton != MouseButtonState.Pressed)
                this.dragStartPoint = null;

            if (this.dragStartPoint.HasValue)
                // XamlWriter.Save() has limitations in exactly what is serialized,
                // see SDK documentation; short term solution only;
                string xamlString = XamlWriter.Save(this.Content);
                DragObject dataObject = new DragObject();
                dataObject.Xaml = xamlString;

                WrapPanel panel = VisualTreeHelper.GetParent(this) as WrapPanel;
                if (panel != null)
                    // desired size for DesignerCanvas is the stretched Toolbox item size
                    double scale = 1.3;
                    dataObject.DesiredSize = new Size(panel.ItemWidth * scale, panel.ItemHeight * scale);

                DragDrop.DoDragDrop(this, dataObject, DragDropEffects.Copy);

                e.Handled = true;

    // Wraps info of the dragged object into a class
    public class DragObject
        // Xaml string that represents the serialized content
        public String Xaml { get; set; }

        // Defines width and height of the DesignerItem
        // when this DragObject is dropped on the DesignerCanvas
        public Size? DesiredSize { get; set; }

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Austria Austria
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190306.1 | Last Updated 29 Feb 2008
Article Copyright 2008 by sukram
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid